gpt4 book ai didi

ios - 以编程方式创建的按钮不响应用户的点击

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:07 24 4
gpt4 key购买 nike

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

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

主.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];
}

登录界面.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);

}

屏幕上没有打印。

提前致谢

最佳答案

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

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

编辑

  - (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);

}

关于ios - 以编程方式创建的按钮不响应用户的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14507507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com