2012-11-30 69 views
1

我读了这个tutorial关于iPhone开发板上的故事板。这家伙通过一个按钮改变了观点。但是按钮的连接是通过界面生成器完成的。我怎样才能以编程方式执行此操作,因此我可以在视图更改之前进行一些检查(例如用户名/密码)? 在此先感谢。iphone - 以编程方式更改按钮的视图

回答

2

连接一个视图与其他(不是按钮与视图)。 将segue更改为custom并为其指定标识符(在IB中)。例如:登录

然后创建一个操作并将其分配给按钮。

在按钮操作使用:

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

创建Segue公司类对象,并在.M使用类似:

UIViewController *dst = [self destinationViewController]; 
UIViewController *nav = [self sourceViewController]; 

//All your custom code 

[nav presentModalViewController:dst animated:YES]; 

编辑:忘记一些重要的事情!

在IB中,在Segue配置中,放入Segue类,创建Segue类的名称。例如:LoginSegue是SEGUE创建的名称,在Segue公司类哟已经写“LoginSegue”

编辑:创建类:

1.-创建扩展UIStoryBoardSegue一个新文件时,会.H是这样的:

#import <Foundation/Foundation.h> 

@interface LoginSegue : UIStoryboardSegue 

@end 

2:在实现中,使用上面的代码称为方法中执行:

-(void)perform 
{ 

UIViewController *dst = [self destinationViewController]; 
UIViewController *nav = [self sourceViewController]; 

//Custom Code 

[nav presentModalViewController:dst animated:YES]; 

} 

如果您需要访问源码的特性Ë的viewController,您需要更改:

UIViewController *nav = [self sourceViewController]; 

eYourClass *nav = [self sourceViewController]; 

希望这有助于!

+1

你能解释一下你说“创建一个Segue类”是什么意思吗? – ozmax

+0

UIStoryBoardSegue的扩展类。要编辑答案添加它 – Vertig0

+0

我看到我必须重写执行方法。任何线索我应该怎么做? (顺便说一句,我使用导航控制器 - 我不想使用模态) – ozmax

0

您可以创建按钮和touchUpInside动作拖动到IBAction在Interface Builder

或代码viewDidLoad

[self.login addTarget:self action:@selector(loginTapped) forControlEvents:UIControlEventTouchUpInside]; 

某处然后loginTapped方法可能看起来像

- (void)loginTapped; 
{ 
    if ([self authenticateWithUsername:self.username.text password:self.password.text]) { 
    [self performSegueWithIdentifier:@"loginSuccessful" sender:self]; 
    } else { 
    // Warn user about invalid username/password 
    } 
} 

这依赖于您在故事板中创建了一个名称与012匹配的segue参数

相关问题