简体   繁体   English

如何获得确定要转到哪个视图控制器的按钮?

[英]How can I get a button to determine which viewcontroller to go to?

I'm trying to use Xcode to take the value of a usertype input and do one of three things: if the usertype is invalid it will stay on the current view controller and tell the user the credentials are invalid, if it is valid and "admin" it will go to the user settings, and if it is "user" or null it will go to the order list. 我正在尝试使用Xcode来获取用户类型输入的值,并执行以下三项操作之一:如果用户类型无效,它将停留在当前视图控制器上,并告诉用户凭据无效,如果有效,则“ admin”将转到用户设置,如果为“ user”或为null,则将转到订单列表。

I already have the if statements built, just not sure how to specify what view controller to go to. 我已经建立了if语句,只是不确定如何指定要转到的视图控制器。

I didn't really understand your question, but can't you simply allocate the view like this: 我不太了解您的问题,但是您不能简单地分配这样的视图:

if(!error) {
   ViewController *viewcont = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
    [self presentViewController:viewcont animated:NO completion:nil];
} else {
 ViewController2 *viewcont2 = [[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];
    [self presentViewController:viewcont2 animated:NO completion:nil];
}

If you are using navigation controller 如果您使用的是导航控制器

if(!error) {
   ViewController *view1 = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
[self.navigationController pushViewController:view1 animated:YES];
} else {
 ViewController2 *view2 = [[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];
[self.navigationController pushViewController:view2 animated:YES];
}

Otherwise use this 否则使用这个

if(!error) {
   ViewController *view1 = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
    [self presentViewController:view1 animated:NO completion:nil];
} else {
 ViewController2 *view2 = [[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];
    [self presentViewController:view2 animated:NO completion:nil];
}

Remember: this solution will not work if your views are not xnib files, I mean if you are using storyboard 请记住:如果您的视图不是xnib文件,则此解决方案将不起作用,我的意思是如果您使用情节xnib

暂无
暂无

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

相关问题 我如何从第三个viewController回到第一个viewController - How can I go back to the first viewController from the third viewController 我怎么知道哪个ViewController是活动的? - How can I know which ViewController is active? 我如何将 .xib ViewController 转到 Storyboard ViewController 并再次将 Storyboard 转到 .xib ViewController - How can i go .xib ViewController to storyboard ViewController and again storyboard to .xib ViewController Swift 3:如何转到上一个ViewController并重新加载它? - Swift 3 : How can I go to the previous ViewController and reload it? 当我按下按钮推送到另一个ViewController时,如何知道哪个indexPath.row我的按钮? - How can I know which indexPath.row my button in when I press a button to push to another ViewController? 如何在XCode中获取ViewController图标? - How can I get the ViewController icon in XCode? 如何确定哪个视图处于活动状态? - How can I determine which view is active? 如何以编程方式确定iOS中已按下哪个选项卡按钮? - How can I programmatically determine which tab button has been pressed in iOS? 如何访问在TabBarViewController中选择的ViewController - How can I access to the ViewController which is selected in TabBarViewController 当我点击没有导航的按钮时,如何在 xib(ViewController) 中打开 xib(ViewController) - How can I open xib(ViewController) in xib(ViewController) when I tap Button without Navigation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM