-3

我以编程方式创建了一个 UIButton 并向其添加了一个功能。它在运行时不响应。

    scrollViewFrame = CGRectMake(10, 110, 300, 40);
    loginBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    loginBut.frame = scrollViewFrame;
    loginBut.backgroundColor = [UIColor whiteColor];
    loginBut.titleLabel.font = [UIFont fontWithName:@"Trebuchet MS" size:33];
    [loginBut setTitleColor:[UIColor Gray] forState:UIControlStateNormal];
    [loginBut setTitle:@"Login" forState:UIControlStateNormal];
    [loginBut setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

    [loginBut addTarget:self
                          action:@selector(checkError)
                forControlEvents:UIControlEventTouchUpInside];
    self.loginBut = loginBut;

    [self.view addSubview:loginBut];

那个行动:

- (void)checkError {
    if ([dispName.text isEqualToString:@""]&&[pw.text isEqualToString:@""]) {
        UIAlertView *alertFailed = [[UIAlertView alloc]initWithTitle:@"\nError" message:@"1. Display Name missing.\n2. Password missing." delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alertFailed show];
    }

}

提前谢谢...

4

3 回答 3

1

([dispName.text isEqualToString:@""]&&[pw.text isEqualToString:@""])

也许这些测试只是失败了。例如,也许dispName.text是零。这与等于空字符串不同。

于 2013-03-27T22:28:03.200 回答
1

看看按钮是否响应首先添加

NSLog("checkError");

在方法的 if 语句之前

- (void)checkError;
于 2013-03-27T22:30:22.747 回答
0

只需在方法中编写一个 NSLog() 即可 - (void)checkError 。如果它打印出来,那么你的情况出了点问题if

否则只是尝试,

 [self.view bringSubViewToFront:loginBut];
 loginBut.userIntractionEnabled = YES;
于 2013-03-27T22:32:36.460 回答