【问题标题】:UIButton won't open new view ControllerUIButton 不会打开新的视图控制器
【发布时间】:2014-05-07 20:19:44
【问题描述】:

按下按钮时,我无法在屏幕上显示新的视图控制器。我创建了一个名为StartUpVC 的UIViewController 子类。我将它添加到应用程序 delegate.m 并创建了一个 StartupVC *startUpVC = [StartUpVC new] 的实例,然后设置了 self.window.rootViewController = startUpVC;。所以现在在StartUpVC 类中,我创建了一个按钮,以便用户可以在后台使用UIImageView 登录。现在,当按下按钮时,它不会为登录屏幕打开视图控制器并且什么也不做。所以我的问题是我如何让它打开那个视图控制器?还是应该只是按下按钮时打开的UIView?

这是在 StartUpVC.m 中

- (void)viewDidLoad
{
    [super viewDidLoad];
 ...Code Irrelevant to the question here...

//Create Log In Button
    UIButton *logInButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    logInButton.frame = CGRectMake(20, 480, 280, 40);
    logInButton.layer.cornerRadius = 4.0f;
    logInButton.layer.opacity = .6;
    logInButton.backgroundColor = [UIColor lightGrayColor];
    [logInButton setTitle:@"Log In" forState:UIControlStateNormal];
    [logInButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logInButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [logInButton addTarget:self action:@selector(sendToLogInScreen:) forControlEvents:UIControlEventTouchUpInside];


    [self.view addSubview:logInButton];

}

-(void)sendToLogInScreen:(id)sender {

    LogInVC *logIn = [LogInVC new];

    [self presentViewController:logIn animated:YES completion:nil];

}

【问题讨论】:

  • 您的 sentToLogInScreen: 方法是否在您按下按钮时被调用?
  • 你在使用故事板吗?
  • 没有故事板,所有 UI 都在代码中完成。
  • 重新考虑不使用情节提要的决定。它们会让你的生活更轻松(在这种情况下,只需从按钮 ctrl 拖动到 LogInViewController)

标签: ios objective-c uiviewcontroller uibutton


【解决方案1】:

您需要知道的第一件事是方法sendToLogInScreen 是否被调用。在要检查的函数中的任意位置放置断点。

一旦你知道函数是否在适当的时间被调用,这个问题要么是:

  • sendToLogInScreen 没有被调用
    • 按钮有问题
      • 没有
      • 手势设置不正确
    • StartupVC 的 view 属性的 UserInteraction 已禁用
  • sendToLogInScreen 被调用
    • logIn 为零
    • presentViewController 因某种原因失败

希望能帮助您找出可能导致问题的原因。

【讨论】:

  • 感谢您的帮助,我会尝试看看其中一种方法出了什么问题。
猜你喜欢
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多