View based application and presentModalViewController

Discussion in 'iOS Programming' started by depl0y, Feb 5, 2009.

  1. depl0y macrumors newbie

    Joined:
    Sep 20, 2008
    #1
    I have searched and searched the forums/internet for an answer to this problem, but all I see is people with the same problems as me.

    I have a "View Based Application" which by default already has a ViewController in it. Great, no problems there. I have a couple of buttons in there and a UITableView.

    The UITableView is filled correctly filled using the normal techniques and everything is OK till this point.

    I created a second NIB and a corresponding UIViewController class. I got the view and class all linked up and ready to go.

    So I included the following code in the class of my first UIViewController, which should slide in the next view.

    PHP:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {     
         
    ReviewsViewControllerreviewController = [[ReviewsViewController allocinitWithNibName:@"ReviewsViewController" bundle:[NSBundle mainBundle]];
         [
    self.navigationController presentModalViewController:reviewController animated:YES];
         [
    reviewController release];
         
    reviewController nil;
    }
    The viewcontroller is instantiated correctly, because when I try to call one of it's methods it actually shows an UIAlertView on top of my first ViewController. The view is just not visible.

    What am I doing wrong here? Is this possible to do? Sure I can remove the first UIViewController from the views and add the next, but I just want to add this view and make it modal.

    Some help would be great!
     
  2. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #2
    presentModalViewController:animated: is a message for a view controller, not for a navigation controller. if you're in a view controller, you should call
    PHP:
         [self presentModalViewController:reviewController animated:YES];
    good luck.
     
  3. depl0y thread starter macrumors newbie

    Joined:
    Sep 20, 2008
  4. CaffeineBuzz macrumors newbie

    Joined:
    Apr 13, 2009
    #4
    How can I apply this to a Tab Based app, without TableView?

    I have a similar situation that I hope someone can help with. I have a tab-based application with 2 tabs, each is of type UIViewController. I've setup FirstView.xib, FirstViewController.c (and .h), SecondView.xib, SecondView.c (and .h). And I've placed a label and button on each of the 2 views in IB. It works fine so far without the buttons doing anything.

    I would like to make the button of the first view activate a third view by using the "presentModalViewController" mentioned in this thread. I've created ThirdView.xib and ThirdViewController.c and .h. I've also implemented a function called "loadThirdNib" in FirstViewController.m which is where I call presentModalViewContoller.

    But my attempts to connect the button's "Touch Up Inside" event to the "loadThirdNib" function in IB are unsuccessful. Have I placed the function in the wrong class implementation? Does it have to go in App Delegate? Or maybe there's something more I need to do in IB to make the connection? :confused: Any guidance here is greatly appreciated. Thank you in advance to anyone who can help out.
     
  5. CaffeineBuzz macrumors newbie

    Joined:
    Apr 13, 2009
    #5
    Ok I was able to hook it up in IB by making my "loadThirdXib" function (IBAction). But now when I click the button in the first view, it crashes. I set a breakpoint at the first line of my "loadThirdXib" function but it doesn't even make it to there. Hmmmm....
     
  6. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #6
    What is the exception you are getting at the crash?
     
  7. CaffeineBuzz macrumors newbie

    Joined:
    Apr 13, 2009
    #7
    Re-enabling shared library breakpoint 1
    2009-04-13 14:13:50.358 TabDemo2[2453:20b] *** -[UIViewController loadThirdXib:]: unrecognized selector sent to instance 0x523630
    2009-04-13 14:13:50.359 TabDemo2[2453:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIViewController loadThirdXib:]: unrecognized selector sent to instance 0x523630'
    2009-04-13 14:13:50.361 TabDemo2[2453:20b] Stack: (
    2421412107,
    2495819323,
    2421441290,
    2421434636,
    2421434834,
    816115430,
    816516406,
    816517630,
    816514372,
    816216343,
    816148479,
    816144864,
    827743722,
    827753484,
    2420913653,
    2420915416,
    827745792,
    827745989,
    816114848,
    816160924,
    9876,
    9730
    )
     
  8. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #8
    what that exception says is that it can't find loadThirdXib:

    could be that you defined
    Code:
    loadThirdXib;
    
    instead of
    Code:
    loadThirdXib:(id)sender;
    
    IBAction's always have a single parameter of type id.
     
  9. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #9
    So here is a problem. This message is being sent to an instance of UIViewController. But it really should be an instance of your particular subclass of UIViewController.

    Most likely you didn't set the class properly in your nib.
     
  10. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #10
    I didn't even notice that :D
     
  11. CaffeineBuzz macrumors newbie

    Joined:
    Apr 13, 2009
    #11
    BlackWolf: Thanks for the input, but it looks right. Here it is:
    - (IBAction)loadThirdXib:(id)sender {

    ThirdViewController* myThirdViewController = [[ThirdViewController alloc] initWithNibName:mad:"ThirdView" bundle:[NSBundle mainBundle]];
    [self presentModalViewController:myThirdViewController animated:YES];

    }

    eddietr: If you mean the class type of "File's Owner" of FirstView.xib in IB, I have it set correctly to "FirstViewController". But it inherits from UIViewController.
     
  12. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #12
    so just to clear it up:
    you have a FirstViewController class that has the loadThirdXib: method. you set that method up as an IBAction, and in IB dragged that action from the file's owner to your button.

    also, you are sure that you never mistyped the method name? because in one of your above posts you talked about "loadThirdNib". also, make sure that you added the method to FirstViewController.h
     
  13. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #13
    OK, but what about the actual instance of the first view controller. That should be in the same nib as your tab bar controller. That instance may not be of the correct type. It looks like it is UIViewController.
     
  14. CaffeineBuzz macrumors newbie

    Joined:
    Apr 13, 2009
    #14
    BlackWolf: I see what you mean about my reference to "loadThirdNib" in an earlier post. Sorry for the confusion. But I searched my project for "loadThird" and only found "loadThirdXib", once in FirstViewController.h and once in FirstViewController.c. Here is the declaration in FirstViewController.h:

    - (IBAction)loadThirdXib:(id)sender;

    eddietr: The actual instance of the FirstViewController is not in my MainWindow.xib. I chose to create a separate xib for it called FirstView.xib. And in FirstView.xib, the class of "File's Owner" is indicated as "FirstViewController", which seems correct to me.
     
  15. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #15
    No, that's actually not your instance. I think that's where you are getting confused. That nib is what your instance uses to set itself up. File's Owner is just a proxy back to the actual instance which is not in the Nib. If it were in that nib, you wouldn't need the proxy.

    But the instance itself is back in your MainWindow.xib probably. Or at least it is somewhere other than FirstView.xib.
     
  16. CaffeineBuzz macrumors newbie

    Joined:
    Apr 13, 2009
    #16
    eddietr: You're absolutely right! I thought that setting the class type of "File's Owner" in "FirstView.xib" was sufficient, but based on your comments, I went back to "MainWindow.xib" and set the ViewController of tab1 to "FirstViewController" instead of UIViewController.

    That did it !!!! Thank you very much both of you for the help. I really appreciate it.
     

Share This Page