Xcode 5使用按钮切换视图控制器

时间:2013-12-30 21:51:24

标签: cocoa-touch

好的我是iOS开发新手,我只想按下我的按钮“LogIn”,如果我的textfield为null,则发送UIAlertView。如果它不为null,则导航到视图控制器“TimeClockView”,但它不导航到TimeClockView。

#import "ViewController.h"
#import "TimeClockView.h"

………………

- (void)LogIn:(id)sender
{
    if ([nameTextField.text  isEqual: @""])
    {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"What's The Issue?"
                                                          message: [NSString stringWithFormat:@"Enter Your Name"]    
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                               otherButtonTitles: nil];
        [message show];
        return;
    }
    else
    {
        TimeClockView *temp = [[TimeClockView alloc]initWithNibName:nil bundle:nil];
        [self.navigationController pushViewController:temp animated:YES]; 

        name = nameTextField.text;
    }

2 个答案:

答案 0 :(得分:0)

您正在使用nil笔尖名称初始化temp对象。需要该参数来知道要实例化哪个ViewController(具有相应的nib)。如果您正在使用nib构建ViewControllers,那么将实例化代码更改为:

TimeClockView *temp = [[TimeClockView alloc] initWithNibName:@"YourNibName" bundle:nil];

“YourNibName”是您的nib文件的名称。

但是,如果您正在使用Storyboard,那么呈现其他ViewControllers的方式完全不同。你需要用故事板研究表演segue。

此外,在比较字符串时,您还需要使用isEqualToString:方法。所以重写if / else语句,如下所示:

if ([nameTextField.text isEqualToString:@""])
{
    //do stuff
}
else {
    //do other stuff
}

答案 1 :(得分:0)

需要执行一个动作来执行segue。 所以我创建了另一个按钮,用它创建了一个segue。 然后在我的原始按钮中使用if语句(IBAction) 去另一个视图控制器