如何以编程方式更改为视图控制器?

时间:2014-05-24 14:38:08

标签: ios objective-c uistoryboard

当我们正确输入密码时,我希望转换到我在main.storyboard中创建的视图。

我该怎么做?

我需要它来提取我已创建的视图。我希望在代码块-(void) unlock中进行转换。


代码

//
//  ViewController.m
//  Login
//
//  Created by Ben Rosen on 5/24/14.
//  Copyright (c) 2014 Ben Rosen. All rights reserved.
//

#import "ViewController.h"
#import "UnlockedScreen.h"

@interface ViewController ()

@end

@implementation ViewController

-(IBAction)login:(id)sender
{

    NSString *correctUser = @"money";
    NSString *correctPassword = @"ilovemoney";

    if ((usernameTextField.text == correctUser)==YES && (passwordTextField.text = correctPassword)==YES)
    {
        [self unlock];
    }

}

-(void) unlock
{
    // here should be the transition
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end

3 个答案:

答案 0 :(得分:2)

在您storyboard中,转到第一个视图控制器,按 CTRL 点击viewController发布 目标视图控制器上的鼠标(和CTRL)。

现在选择segue的类型push如果你在navigationControllermodal)和选择标识符检查员用于此触发器

enter image description here

在您的代码中:

-(IBAction)login:(id)sender
{

    NSString *correctUser = @"money";
    NSString *correctPassword = @"ilovemoney";

    if ([usernameTextField.text isEqualToString:correctUser] && [passwordTextField.text isEqualToString:correctPassword])
    {
        [self performSegueWithIdentifier:@"name" sender:sender];
    }

}

如你所见,我改变了你的if语句..你的比较是错误的。

答案 1 :(得分:1)

[self unlock];

删除它并编写此方法

[self performSegueWithIdentifier:@"profileSegue" sender:self] ;

在检查员

上的标识符(profileSegue)的故事板名称中
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


    if ([segue.identifier isEqualToString:@"profileSegue"]) {
      ProfileViewController *profileViewController = segue.destinationViewController;
      profileViewController.isFromDealView = YES;
      profileViewController.hidesBottomBarWhenPushed = YES;
}

答案 2 :(得分:1)

使用您为viewController提供的标识符实例化viewController。

-(void) unlock
{
    // here should be the transition
    UIViewController *vc = [[UIStoryboard storyboardWithName:@"storyboardname" bundle:nil] instantiateViewControllerWithIdentifier:@"viewControllersID"];
    [self presentViewController:vc animated:YES completion:nil];
}