开发者

iPhone multiple view flow question

开发者 https://www.devze.com 2023-03-12 08:32 出处:网络
I have a root view controller which contains an outlet for my login view controller. the root view should control the flow to the next view, yet my login view has the button to continue. how woul开发者

I have a root view controller which contains an outlet for my login view controller. the root view should control the flow to the next view, yet my login view has the button to continue. how woul开发者_开发问答d I set the button's touch up inside to the IBAction in my root controller?

One method I have though of was keep a pointer to the root class where I create the login class (new code is commented out):

// RootViewController.m

- (void)viewDidLoad {
    LoginViewController *loginController = 
        [[LoginViewController alloc]initWithNibName:@"LoginView" bundle:nil];
    self.loginViewController = loginController;
    //loginViewController.parent = self;
    [self.view insertSubview:loginController.view atIndex:0];
    [loginController release];
    [super viewDidLoad];
}

- (IBAction)loginPressed: (id)sender
{
    self.loginViewController.loginButton.enabled = NO; //yea... doesnt work
}

So in IB I add a UIViewController to the Nib and give it parent as an outlet, and then assign button's touch up inside event to loginPressed which is defined in the root controller (parent)... this didnt work so well explicitly refering to the controls from self.loginViewController in the RootViewController.

is there a correct way to do this.

-frustrated c++ / c# / java coder


Have you considered presenting the loginView modally? Then you can pop the view and be back at the rootViewController and move on from there.


Create the button pressed method outlet in the same class as that XIB and hook it up. I assume your login view is created by the root controller... create an assign property for the root view on the login view, and then assign the root controller to that when creating the login view. Then, in the login view's method for "button pressed" you can reference the root view controller. Spelled out:

in LoginController's .h file:

@property (nonatomic, assign) RootViewController* rvc;

in LoginController's .m file:

@synthesize rvc;

in Root View Controller's "make the login controller code", after you've initialized it but before you've presented it:

[loginController setRvc:self];

in your LoginController's button touched method:

[[self rvc] whateverMethodThatDoesLoginStuff];

This way you have your path between when the user presses the button and your "do-login" code.


You can have a loginViewController as an IBOutlet, connect its action 'touch up inside' to -loginPressed: of file owner.

Or you can alloc and init your controller in -viewDidLoad programmatically all by yourself, and set the action programmatically as well.

But what you did seems to mix the two ways.

0

精彩评论

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