繁体   English   中英

iphone - 以编程方式使用按钮更改视图

[英]iphone - change view with button programmatically

我在 iphone dev 上阅读了有关故事板的教程 这家伙通过一个按钮改变视图。 但是按钮的连接是通过界面构建​​器进行的。 我如何以编程方式执行此操作,以便在更改视图之前进行一些检查(例如用户名/密码)? 提前致谢。

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

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

在按钮动作中使用:

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

创建一个 Segue Class 对象并在 .m 中使用类似的东西:

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

//All your custom code

[nav presentModalViewController:dst animated:YES];

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

在IB中,在Segue configuratión中,放入Segue Class,创建的Segue Class的名称。 例如:LoginSegue 是创建的 segue 的名称,在 Segue 类中你必须写“LoginSegue”

编辑:创建类:

1.- 创建一个扩展 UIStoryBoardSegue 的新文件,.h 将类似于:

#import <Foundation/Foundation.h>

@interface LoginSegue : UIStoryboardSegue

@end

2.- 在实现中,在名为 perform 的方法中使用上面的代码:

-(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];

希望这有帮助!

您可以创建按钮并将 touchUpInside 操作拖动到界面构建器中的IBAction

或在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
  }
}

这取决于您在情节提要中创建了名称与identifier参数匹配的 segue

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM