3

I have a split-view app with a button in the detail view that, when clicked, will take the user to a full screen view of the selected image.

I understand that I need a new nib file and view controllers, but I'm not sure how to connect these new files with my existing RootViewController and DetailViewController files.

I know this is really vague, but any help at all would be most appreciated.

Karoly S
  • 3,180
  • 4
  • 35
  • 55
ebeth
  • 153
  • 1
  • 3
  • 13
  • 1
    It sounds like it's not just nib files but view controllers that you're unfamiliar with. Check out the [View Controllers Programming Guide](http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457) – bshirley Aug 04 '11 at 19:11

1 Answers1

6

As long as you are moving to a single view(as in not another split view) you should only need one more view controller. If I understand what you are doing, then the progression should be something along the lines of:

  1. Create a new view controller with associated .xib file.
  2. Declare the specific instance of your new view controller, in this case called newViewController, in DetailViewController.h and synthesize it in DetailViewController.m

    @interface DetailedViewController
    {    
        NewViewController *newViewController;
    }
    @property (nonatomic, retain) NewViewController *newViewController;
    @end
    
  3. Add your IBAction to the header file of DetailViewController, this will be the function responsible for actually triggering your view switching

  4. Implement the view switch action in your DetailViewController.m file, should look something like this:

    (IBAction)switchToNewView:(id)sender
    {
        if (newViewController == nil)
        {
            NewViewController *newViewController =
              [[NewViewController alloc] 
                initWithNibName:@"NewViewController"
                bundle:[NSBundle mainBundle]];
    
            self.newViewController = newViewController;
        }
    
        // How you reference your navigation controller will
        // probably be a little different
        [self.navigationController
           pushViewController:self.newViewController
           animated:YES];
    }
    
  5. Then in your DetailViewController.m file inside of the viewDidLoad function add the following:

    UIBarButtonItem *addButton =
      [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem: UIBarButtonSystemItemAdd
        target:self action:@selector(switchToNewView:)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
    
  6. The other option that you have if you choose to implement this through a UIButton, is to go create the button in Interface Builder on your NewViewController.xib, then select it, and in the Connections inspector, create a link between the "touchUpInside" event and the file owner, and then select your switchToNewView IBAction. This should accomplish the same thing.

Thats the general idea. I hope that helps!

EDIT: As asked in the comments, if adding a button as a UIBarButton as part of a navigation controller you would simply need to do something like below:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(switchToNewView:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
Mat
  • 202,337
  • 40
  • 393
  • 406
Karoly S
  • 3,180
  • 4
  • 35
  • 55
  • This is really helpful. Thank you so much! A couple questions though I keep getting errors in my DetailViewController.m file that newViewController is undeclared Request for member 'newViewController' in something not a structure or union Request for member 'newRouteViewController' in something not a structure or union Also, I'm not sure what step 4 does. Is it to just set up the button? I have a button already set up so how would I incorporate the line UIBarButtonSystemItemAdd target:self action:@selector(switchToNewView:)]; into what I already have? – ebeth Aug 04 '11 at 20:33
  • I suppose the naming is a little confusing, newViewController is just what I happened to call my custom view controller that I added and plan to switch to. You would call it whatever that new view controller you just added is called. If you did call it NewViewController, though not ideal because its not terribly descriptive, then be sure that you have included your header file in DetailViewController. Step 4 is how you pragmatically add a navigation button and assign your IBAction to it. If you are doing this through a UIButton let me know, and I will tell you how to do it that way. – Karoly S Aug 04 '11 at 20:39
  • ok, if I called it FullViewController would that alter the name of newRoutViewController? Should I have declared fullViewController somewhere before using it in the switchToNewView function in the DetailViewController? and yes, I am using a UIButton. – ebeth Aug 04 '11 at 20:45
  • Yes. XCode defaults the name of your class to the name of the file name, so in your case you would declare fullViewController as your viewController instance, of type FullViewController. Yes I edited my previous post as I didn't specify that, see step 2. In the case of a UIButton see step 6. Let me know if you need more clarification, and be sure to mark the answer if you manage to get it implemented! – Karoly S Aug 04 '11 at 21:02
  • Do i need to import the header file of my new View Controller in the header file of the detail View controller? Also I keep getting the following error `Request for member 'newRouteViewController' in something not a structure or union` which I can't get rid of. Other than that it seems to be working just fine. – ebeth Aug 04 '11 at 21:17
  • Yes you do need to import the header file. Have you declared the @property for newViewController and the synthesized it in the .m file? If you newRouteViewController from my code above that was a miss edit on my part, it should be the instance name of the view controller you want to switch to. – Karoly S Aug 04 '11 at 21:20
  • Yep, that was it. Yes I have declared the @property and also synthesized. I get it to run now!! but clicking the button doesn't change the view. Working on figuring out why now. – ebeth Aug 04 '11 at 21:23
  • Make sure that you have done the connection linking in IB. If you have, add a breakpoint into your switch view function and see if its being called, start stepping through it and see what happens. – Karoly S Aug 04 '11 at 21:30
  • I think I have linked everything. The button is linked to the function and everything else was already linked. `self.fullViewController = fullViewController; //i get a warning here saying that "Local declaration of 'fullViewController' hides instance variable"` besides that I still don't even go to the new view. I stepped through the function and it seems like it should work, but it doesn't. Not sure what else to try. – ebeth Aug 04 '11 at 21:50
  • Hmm,this means that you are declaring your view, fullViewController, twice, which is not a good thing, you don't want the local declaration in this case, you want the one declared in your .h file. Keep looking around, maybe look at some samples, you can't be far off. – Karoly S Aug 04 '11 at 21:57
  • How would this line differ? Is there a different way for me to reference my navigation controller? `[self.navigationController pushViewController:self.newViewController animated:YES];` – ebeth Aug 05 '11 at 14:12
  • Chances are yes. I can't help you a whole lot since I don't know how you structured your project. What that line is doing is switching the active view based on the views that are available from your window, and animating the switch. – Karoly S Aug 05 '11 at 14:44
  • If I were to use a UIBarButtonItem to do this, how would that change things? Would I still need to declare it in my DetailViewController.h file as `IBOutlet UIBarButtonItem *fullViewButton;` ? – ebeth Aug 09 '11 at 01:14
  • No, you dont need to do that. See the code snippet that I've added to the bottom, its actually much simpler! – Karoly S Aug 09 '11 at 14:36
  • ok everything should be working, but my navigation controller is null and that is what is preventing the new view from loading. any ideas on why it could be null? – ebeth Aug 11 '11 at 00:39
  • If you are starting from the template SplitView App provided by Apple it will be because it isn't declared anywhere. UISplitViewControllers and NavigationControllers don't play very well together unfortunately. You might want to just ask a new question and provide more code and give me the link, I'd be happy to help. The mods generally don't like long drawn out comment conversations like this :P – Karoly S Aug 11 '11 at 07:28