gpt4 book ai didi

iphone - 以编程方式将 UIButton 添加到 Storyboard View

转载 作者:行者123 更新时间:2023-11-29 11:09:57 24 4
gpt4 key购买 nike

我有一个 Storyboard View Controller (带有我自己的自定义子类),我有一个 initWithCoder 自定义方法,我想在其中使用我自己的自定义对象来流行 View (我想保持以编程方式执行此操作的灵 active 。

代码工作正常,直到 [self.view addSubview:button]; 被调用,然后崩溃并显示:“Could not load NIB in bundle”。

我只想在我的 View 上添加一个 UIButton...

- (id)initWithCoder:(NSCoder*)aDecoder 
{
if(self = [super initWithCoder:aDecoder])
{
[self initLoginForm];
}
return self;
}

-(void)initLoginForm
{

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 170, 100, 30);
[button setTitle:@"Login..." forState:UIControlStateNormal];
[button addTarget:self action:@selector(handleLoginAttempt) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

}

最佳答案

如何将按钮放在 viewDidLoad 方法中,然后将其添加到 initLoginForm 中。

像这样:

-(void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 170, 100, 30);
[button setTitle:@"Login..." forState:UIControlStateNormal];
[button addTarget:self action:@selector(handleLoginAttempt) forControlEvents:UIControlEventTouchUpInside];
}

-(void)initLoginForm {

[self.view addSubview:button];

}

或者在viewDidLoad中添加然后隐藏,然后在initLoginForm中显示。

关于iphone - 以编程方式将 UIButton 添加到 Storyboard View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11998555/

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