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 thi开发者_Python百科s 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:
- Create a new view controller with associated .xib file.
- 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
- Add your IBAction to the header file of DetailViewController, this will be the function responsible for actually triggering your view switching 
- 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]; }
- 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];
- 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];
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论