[Help Please] Calling Outlets from another ViewController or Class?

Discussion in 'iOS Development' started by cocotutch, Sep 18, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Hi there,

    I am stuck on this ONE thing, I have resorted to the Docs and have solved my previous issues with them, but this one seems to elude me.

    I started a project with the "Utility Application" template in Xcode and I did all my main stuff in the "MainViewController", but I want to have an action in the "FlipSideViewController" that erases a text field in the MainView.

    I have tried in FlipSideViewController;

    Code:
    @class MainViewController;
    #import "MainViewController"
    
    and then calling MPeriodOne.text = [NSString stringWithFormat:@""];
    the code to erase the text field works in ALL my other projects, but I just need to know how to erase "MPeriodOne" from the FlipSideViewController.

    Can anyone help me?

    cocotutch
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Option 1) Use the NSNotificationCenter.
    Option 2) You have a root view controller that controls each view controller, right? Call a method on it that in turn calls a method on your other view controller.
    Option 3) Have the applicationDelegate take care of everything.
    Option 4) Create your second view controller with a pointer to the first view controller. A pointer, not a new object. Call the method on it.

    In the first three, the two view controllers don't know about each other. In the last one, the second controller is aware of the first, but not vice versa.
  3. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Not understanding....Check out the Utility Template in Xcode, I don't get this at all. You will.

    FlipSideViewController.h:

    [OBJC]@protocol FlipsideViewControllerDelegate;
    @interface FlipsideViewController : UIViewController {
    id <FlipsideViewControllerDelegate> delegate;

    IBOutlet UIButton *clear;
    }



    @property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
    - (IBAction)done;
    - (IBAction)clearAll

    Please Register or Log in to view images

    id)sender;

    @end


    @protocol FlipsideViewControllerDelegate
    - (void)flipsideViewControllerDidFinish

    Please Register or Log in to view images

    FlipsideViewController *)controller;
    @end[/OBJC]

    How do I link it up to "MainViewController" and call TextFields like so "MP.text =....etc"?
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    There's nothing to not understand. I gave you four very viable options that will solve your problem.

    Also, if you can't understand the Utility template, you shouldn't be making apps.
  5. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I should be making apps mate, don't you ever speak to another person like that. It's just very weirdly linked up. Maybe I should just circumvent the template and stick to my main view. Thanks for nothing.
  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    The template is not weirdly linked up at all. There is a RootViewController that controls the other views. Exactly the way MVC prescribes.

    Part of my disdain for your app making comes from the fact that you seem to ignore everything I suggest, although it clearly works (especially in instances such as this where I presented four perfectly viable options). But, since you want everything spelled out for you, I'll expand my first response.

    Option 1) Have your main view controller subscribe to notifications via the NSNotificationCenter. Have your second view controller send these notifications via the NSNotificationCenter. When the notification is received by the first controller, set the string on the textfield.

    Option 2) ((RootViewController *)parentViewController).textField.text = @"String"; Or [parentViewController setTextfieldText

    Please Register or Log in to view images

    "String"]; This will either refrence a property on your root view controller, or call a method in your root view controller that winds up setting the text in your text field.

    Option 3) [(YourAppDelegate *)[[UIApplication sharedApplication] delegate] setString

    Please Register or Log in to view images

    "String"]; This calls a method in your app delegate that in turn tells the root view controller to set the string in the text field contained within firstViewController.

    Option 4) Alter your second view controller's initialization method to be -initWithNibOrNil:bundle:andSibling

    Please Register or Log in to view images

    UIViewController *)sibling; In it, _sibling = sibling. Later, ((FirstViewController *)_sibling).text = @"String";


    Although, I *really* wish you would actually take a look at the advice you are given instead of just ignoring it until you hear what you want to hear.
  7. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Okay, thankyou for elaborating. But you mention a Root View Controller. Where is that?
    Picture 3.png

    I modified this:

    ((RootViewController *)parentViewController).textField.text = @"String";

    to:

    ((MainViewController *)parentViewController).MPeriodOne.text = @"";

    And it is saying "undeclared parentViewController" when I build, I have even added a:

    @int...

    MainViewController *parentViewController;

    EDIT: I changed it to: [parentViewController clear:nil]; after fiddling around, but it still won't erase the fields. It compiles with no errors.

    cocotutch
  8. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I owe you an apology. Apple changed the Utility Template to display this new view controller property in 3.0:
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    That said, everything I stated can be easily applied to what you are doing with minimal to no change. Also, I see the new template works with protocols, which is ridiculous for this app. While completely ridiculous for this application, it does offer you one more option that is even easier then the first few I gave you.

    In FlipsideViewController.h
    [OBJC]/* Change this */
    @protocol FlipsideViewControllerDelegate
    - (void)flipsideViewControllerDidFinish

    Please Register or Log in to view images

    FlipsideViewController *)controller;
    @end

    /* To this */
    @protocol FlipsideViewControllerDelegate
    - (void)flipsideViewControllerDidFinish

    Please Register or Log in to view images

    FlipsideViewController *)controller;
    - (void)flipsideViewControllerDidChangeTextfieldTextTo

    Please Register or Log in to view images

    NSString *)text;
    @end
    [/OBJC]


    In FlipsideViewController.m
    [OBJC]/* Call this when you want to change the text */
    [self.delegate flipsideViewControllerDidChangeTextfieldTextTo

    Please Register or Log in to view images

    "SomeString"];
    [/OBJC]

    And finally, in your MainViewController.m, implement the method.
    In MainViewController.m
    [OBJC]/* Add this method anywhere */
    - (void) flipsideViewControllerDidChangeTextfieldTextTo

    Please Register or Log in to view images

    NSString *)text {
    yourTextField.text = text
    }
    [/OBJC]
  9. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Apology Accepted, and thankyou.
  10. yujean

    yujean New Member

    Joined:
    Apr 5, 2010
    Messages:
    1
    Likes Received:
    0
    Re: Dismissing modal view controller from nested view controller...

    Heya,

    I noticed that you guys were discussing the template for a "Utility Application".

    I'm having the hardest time right now trying to dismiss the FlipsideViewController from a nested viewcontroller:

    MainViewController creates a FlipsideViewController, and sets the flip's delegate to self.

    My FlipsideViewController creates a Navigation Controller (called navController), and also creates a "TestimonialViewController". The first and only object in navController's view controller array is the "testViewController".

    Code:
    TestimonialListViewController *testListViewController = [[TestimonialListViewController alloc] initWithNibName:@"TestimonialListViewController" bundle:[NSBundle mainBundle]];
    
    // Set up our navigation controller, with "testListViewController" as the starting viewController
    navController.viewControllers = [[NSArray arrayWithObject:testListViewController] retain];
    [self.view addSubview:navController.view];
    The "testViewController" changes the title and tint of the navigationBar:
    Code:
    self.title = @"Wall of Fame";
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6 green:0.75 blue:0.6 alpha:1.0];
    The "testViewController" also adds a button to that navigation bar. This button is intended to dismiss the modal view controller...
    Code:
    // Add a button to dismiss this modal view controller.
    UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(done)] autorelease];
    self.navigationItem.rightBarButtonItem = doneButton;
    My understanding is that I need to dismiss the MVC from the MainViewController (since it's the view controller that presented the MVC).

    What is the tactic I should pursue??? I have tried creating an instance of "FlipsideViewController" in my testViewController, but that didn't work (and wouldn't make sense) because MVC's cannot dismiss themselves.

    Should I be trying to present the MVC from the AppDelegate, and then dismiss it from AppDelegate the same way?

    This has been killing me for like 6 hours. Please help!

    - Eugene
    ------------------double post merged------------------
    Got it.

    Code:
    TheNameAppDelegate *appDelegate = (TheNameAppDelegate*)[[UIApplication sharedApplication] delegate];
    	[appDelegate.mainViewController dismissModalViewControllerAnimated:YES];

Share This Page