我知道这种问题在这里已多次讨论过,但我已经搜索过并试过没有成功。我试图在常规菜单的顶部创建一个简单的登录视图。此登录页面包含2个文本字段(用户名和密码)和1个按钮(登录)。我的问题是,当我点击“登录”按钮时,一切看起来都很完美,但没有响应。
* 使用moveTo方法从开始到结束点设置动画的登录视图。评论该部分但仍无响应。
的main.m
- (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];
}
LoginScreen.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);
}
屏幕上没有任何内容。
提前致谢
答案 0 :(得分: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
可能是更好的选择。
答案 1 :(得分: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);
}
答案 2 :(得分: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];
来自init方法的。