开发者

iOS: Beginner Help for my first app: Switch views?

I am writing my first sinful iPhone app. My login screen is ready I thinK. I implemented a View in my MainWindow for login-credentials and add all UI to the view (text boxes etc).

Now what?

I want to check my credentials and if there are okay I will kill the login-view from the main window and show the main view with all information.

How to do?

Can I easily add the button-event in the loginviewcontroller, check the logindata there and get access to the main win开发者_Python百科dow and kill from there on the view itself without problems? Or must I declare the button-event from the view in the main window and do all things there?

My Code:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andDelegate:(id)delegate
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(self.view.frame.size.width / 2 - 55.0f, self.view.frame.size.height / 2 + 85.0f
                               , 110.0f, 35.0f);
        [button setTitle:@"title" forState:UIControlStateNormal];
        [button addTarget:delegate action:@selector(yourMethodInMainWindow:) forControlEvents: UIControlEventTouchDown];
        [self.view addSubview:button];
    }
    return self;
}


Check this iPhone View Switching Tutorial


in your viewcontroller:

//maybe you will need to have more arguments added to you init method
//for instance you might be using -(id)initWithNibName:bundle: right now
//then just add delegate to it like -(id)initWithNibName:bundle:andDelegate:
//just to clarify: delegate = reference to class you want to call a method in
-(id)initMethod:(id)delegate {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.button.frame = CGRectMake(self.view.frame.size.width / 2 - 55.0f, self.view.frame.size.height / 2 + 85.0f
                                     , 110.0f, 35.0f);
    [button setTitle:@"title" forState:UIControlStateNormal];
    [button addTarget:delegate action:@selector(yourMethodInMainWindow:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

in class with mainWindow:

-(void)yourMethodInMainWindow:(id)sender {
    //do whatever you want to do.
}

that's one way to set it up programmatically. if u are using nib then u will define an method in your .h-file och type - (IBAction)buttonClicked:(id)sender and then connect these in interface builder.

i guess you know how to push a new view. could be done with for instance presentModalViewController:animated:. all of this is already on stackoverflow and google.

the answer here might tell u what u need to know about accessing the UIApplication and thereby the window.


You should use navigation controller for transformation between different views.

First of all, insert a navigation controller into the MainWindow.xib

Then, load LoginViewController in that navigation controller. LoginviewController will be the view in which ur login id and password and login btn will be placed.

Now, for login btn action, write a method,

- (IBAction) loginBtnPressed : (id) sender{

     MainViewController *newObject = [MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];

    [self.navigationController pushViewController:newObject animated:YES];

}

After you write this method, just attach this method to the btn in your .xib file in interface builder.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜