开发者

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:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜