Call rootViewController to switch views in content view (iOS)

I am working on a fairly simple multi-user app for iOS, and I am following a great tutorial in the Apress book . I basically got my rootViewController created and displayed along with the application delegate, and I have several contentControllers (6) that I would like to change and enter based on user input. However, in the book, they perform their switches using the toolbar button placed in rootView using the Interface Builder. It runs a method in rootView that loads the new ViewController content and displays it.

My problem is that I would like to execute the content view switch (which is in my rootViewController instance), but I would like to trigger the switch action using the button that is in my content view (and therefore not available as my file owner is my ContentViewController, the link to which is contained inside my rootViewController).

Hope I explained it well enough, please let me know if I need to clarify more. I appreciate any help!

+3
source share
2 answers

(RootViewController * rootViewController) : self.contentView.rootViewController = self;.

, : [self.rootViewController switchView]. , ( IBAction).

, : 1) RootViewController

@class RootViewController;

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

@property (retain) RootViewController *rootViewController;

, . 2) , :

@implementation MyContentViewController

@synthesize rootViewController;

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

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

.

3) RootViewController:

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

. , .

+5

, IBAction , :

 [self.parentViewController switchToDifferentController:(int) viewNumber]

switchToDifferentController . , parentView , .

, , , , .

, "" , - . , "". . : " ", . / . UITableView, , , .

, : (.

  • ,
  • delegate
  • , .
  • , ,

@protocol myVCDelegate
    - (void)switchToDifferentController:(int) viewNumber ;
@end


@interface ParentViewController : UIViewController <VCDelegate>

@property(nonatomic, assign) id <VCDelegate> delegate

childController.delegate = self;

[self.delegate switchToDifferentController:kController5];
+2

Source: https://habr.com/ru/post/1791776/


All Articles