iPhone – Call RootViewController to switch views in the content view (iOS)

I am making a very simple multi-view application for iOS, and I have been following a great tutorial in the Apress book. I have basically instantiated my rootViewController and Displayed with the app delegate, and I have a lot of content viewControllers(6) I want to swap in and out based on user input. However, in this book, they use Interface Builder to perform a switch on the toolbar on the rootView. It triggers one in the rootView Method, the method loads the new content ViewController and displays it.

My problem is that I want to perform content view switching (located in my rootViewController instance), but I want to use my content view The button that triggers the switch operation (so it is not available as my file owner) is my contentViewController, and its reference is saved in my rootViewController.

I hope I have explained it well, please tell me if I should Detailed instructions. I appreciate any help!

when you create a content view in a custom init method, or just after creating a content view To assign it, you need to pass a reference to the root view controller (RootViewController * rootViewController): self.contentView.rootViewController = self;.

Now, in the content view, you can in the root view Call the corresponding method in the controller to perform the switch: [self.rootViewController switchView]. Then you can trigger this call (IBAction method) in the method that is called when the button is pressed.

So this is what you need to do :
1) Create a property in the content view controller of RootViewController type

@class RootViewController;

@interface MyContentViewController: NSObject {
@private
RootViewController *rootViewController;
}

@property (retain) RootViewController *rootViewController;

and make sure it keeps reference.
2) Synthesize attributes and add callbacks to the root view controller of the switch view:

@implementation MyContentViewController

@synthesize rootViewController;< br />
- (IBAction) switchView:(id) sender {
[rootViewController switchToNextView];
}

-(void) dealloc {
[rootViewController release];
[super dealloc];
}

Same Post your reserved reference at the end.

3) Assign the root view controller to the content view in RootViewController:

self.contentViewController = [ [[MyContentViewController alloc]
initWithNibName:@"ContentView"
bundle:nil] autorelease];
self.contentViewController.rootViewController = self;

This should be all. I hope to help you.

I am making a very simple multi-view application for iOS, and I have been following a great tutorial in the Apress book. I Basically my rootViewController has been instantiated and displayed with the app delegate, and I have a lot of content viewControllers(6) I want to swap in and out based on user input. However, in this book, they use Interface Builder’s toolbar on the rootView Execute switch on it. It triggers a method in rootView that loads the new content ViewController and displays it.

My problem is that I want to perform content view switching (located in my rootViewController instance) , But I want to use the button in my content view to trigger the switch operation (so it is not available as my file owner) is my contentViewController, and its reference is saved in my rootViewController.

I hope I have Very well explained, please tell me if I should elaborate. I appreciate any help!

When you create a content view in a custom init method, or just assign it after the content view is created, you need to pass to the root view controller ( RootViewController * rootViewController) reference: self.contentView.rootViewController = self;.

Now, in the content view, you can call the corresponding method in the root view controller to perform the switch:[ self.rootViewController switchView]. Then you can trigger this call (IBAction method) within the method that is called when the button is pressed.

So this is what you need to do:
1) In the RootViewController type content Create a property in the view controller

@class RootViewController;

@interface MyContentViewController: NSObject {
@private
RootViewController *rootViewController;
}

@property (retain) RootViewController *rootViewController;

and make sure it keeps the reference.
2) Synthesize the properties and add the callback to Switch the root view controller of the view:

@implementation MyContentViewController

@synthesize rootViewController;

- (IBAction) switchView :(id) sender {
[rootViewController switchToNextView];
}

-(void) dealloc {
[rootViewController release];
[super dealloc ];
}

At the same time, publish your reserved reference at the end.

3) Assign the root view controller to the content view in RootViewController:< /p>

self.contentViewController = [[[MyContentViewController alloc]
initWithNibName:@"ContentView"
bundle:nil] autorelease];
self. contentViewController.rootViewController = self;

This should be all. I hope to help you.

Leave a Comment

Your email address will not be published.