2

我知道这种问题在这里讨论过几次,但是我已经搜索并尝试过,但没有成功。我正在尝试在常规菜单顶部创建一个简单的登录视图。此登录页面包含 2 个文本字段(用户名和密码)和 1 个按钮(登录)。我的问题是,当我单击登录按钮时,一切看起来都很完美,但没有响应。

*登录视图使用 moveTo 方法从起点到终点进行动画处理。评论那部分,但仍然没有回应。

主程序

- (void)viewDidLoad
{

    prefs = [NSUserDefaults standardUserDefaults];

    if (![prefs objectForKey:@"userName"]) {
        LoginScreen *login = [[LoginScreen alloc] initWithFrame:CGRectMake(35, 400, 250, 350)];
        [self.view addSubview:login];
        [login moveTo:CGPointMake(35.0, 65.0) duration:0.6 option:UIViewAnimationOptionCurveEaseOut];  
    }        

    [super viewDidLoad];
}

登录屏幕.m

@synthesize login;

    - (id)initWithFrame:(CGRect)frame
    {
         self = [super initWithFrame:frame];
         if (self) {

             self.layer.masksToBounds = YES;
             self.userInteractionEnabled = YES;

             [self addButton:login withTitle:@"Login" andSize:CGRectMake(85, 200, 90, 35)];
             [login addTarget:self action:@selector(attemptLogin)
                   forControlEvents:UIControlEventTouchUpInside];

         }

         return self;
    }

        - (void) addButton: (UIButton*) button withTitle: (NSString*) title andSize: (CGRect) size{

            button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = size;
            [button setTitle:title forState:UIControlStateNormal];
            button.userInteractionEnabled = YES;
            [self addSubview:button];
        }


        - (void)attemptLogin{

            NSString *user = usernameTxt.text;
            NSString *pass = passwordTxt.text;
            NSString *url = [[NSString alloc] initWithFormat:@"http://domain.com/login.php?username=%@&password=%@", user, pass];

            NSLog(@"%@", url);

        }

屏幕上没有打印任何内容。

提前致谢

4

3 回答 3

2

这是因为您创建了一个按钮,但没有指定单击按钮时要调用的选择器。将此行添加到您的addButton方法将解决问题:

[button addTarget:self action:@selector(attemptLogin) forControlEvents:UIControlEventTouchUpInside];

当您尝试通过综合login属性执行相同操作时,代码不起作用,因为您从未设置login. 当您将它传递给addButton函数时,该值将被忽略(您立即重新分配它)。但是,赋值永远不会改变 的值login,因为 Objective C 是按值传递参数的。

修复代码的另一种方法是将指针传递给login,而不是login自身,如下所示:

- (void) addButton: (UIButton**) button withTitle: (NSString*) title andSize: (CGRect) size {
    *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    (*button).frame = size;
    [*button setTitle:title forState:UIControlStateNormal];
    (*button).userInteractionEnabled = YES;
    [self addSubview:*button];
}

我建议不要使用这种修复代码的方式:login直接在内部使用addButton可能是更好的选择。

于 2013-01-24T17:49:07.953 回答
2

问题主要是您正在分配一个新按钮,因此无法使用您的登录按钮,我在您的代码上评论了分配,这应该可以工作:

//旧版代码已删除,请检查编辑

编辑

  - (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.layer.masksToBounds = YES;
        self.userInteractionEnabled = YES;

        login = [self addButtonWithTitle:@"Login" andSize:CGRectMake(85, 200, 90, 35)];
        [login addTarget:self action:@selector(attemptLogin)
        forControlEvents:UIControlEventTouchUpInside];

    }

    return self;
}

- (UIButton) addButtonWithTitle: (NSString*) title andSize: (CGRect) size{

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = size;
    [button setTitle:title forState:UIControlStateNormal];
    button.userInteractionEnabled = YES;
    [self addSubview:button];
    return button;
}


- (void)attemptLogin{

    NSString *user = usernameTxt.text;
    NSString *pass = passwordTxt.text;
    NSString *url = [[NSString alloc] initWithFormat:@"http://domain.com/login.php?username=%@&password=%@", user, pass];

    NSLog(@"%@", url);

}
于 2013-01-24T17:49:56.697 回答
1

我认为你的意思是:

- (void) addButton: (UIButton*) button withTitle: (NSString*) title andSize: (CGRect) size{

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = size;
    [button setTitle:title forState:UIControlStateNormal];
    button.userInteractionEnabled = YES;
     [button addTarget:self action:@selector(attemptLogin)
           forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
}

您还应该删除:

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

从初始化方法。

于 2013-01-24T17:50:00.790 回答