简体   繁体   中英

iOS - Adding a new view / Switching views on button press

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.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM