在自定义视图中动态创建 UIButton - Objective C
我正在创建一个自定义视图,其作用类似于页脚栏,因此我可以传递多个视图,而不是使用 IB 添加等,从而节省时间并学习更多编码。
我的类首先创建背景,然后我向其中添加一个 UIButton,但由于某种原因,文本没有显示,而且单击也没有触发。
自定义类 h 文件:
@interface menuViewone : UIView {
IBOutlet UIView *menuback;
}
-(void)nextPage;
-(void)menubackground;
-(void)nextButton;
我的自定义类 m 文件:
-(void)nextPage{
NSLog(@"Next Page Clicked");
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)menubackground{
NSLog(@"Menu Background Loaded");
menuback = [[UIView alloc] initWithFrame:CGRectMake(0, 410, 320, 50)];
[menuback setBackgroundColor:[UIColor yellowColor]];
[self addSubview:menuback];
[menuback release];
[self nextButton];
}
-(void)nextButton{
NSLog(@"Create Next Button");
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
nextBtn.frame = CGRectMake(0, 410, 60, 50);
[nextBtn setBackgroundColor:[UIColor redColor]];
nextBtn.titleLabel.text = @"next";
nextBtn.titleLabel.textColor = [UIColor blackColor];
[nextBtn setTitle:@"Next" forState:UIControlStateNormal];
[nextBtn addTarget:self action:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:nextBtn];
}
我在主 m 文件中调用它,使用:
#import "menuViewone.h"
.......
- (void)viewDidLoad
{
menuViewone *menu = [[menuViewone alloc] init];
[menu menubackground];
[self.view addSubview:menu];
[menu release];
[super viewDidLoad];
}
所以目前我得到一个黄色页脚背景,左侧有一个红色按钮,但没有操作或标签
1:发生了什么? 2:我这样做对吗?
更新 我已经解决了当我释放按钮时未显示文本的问题,该按钮将自动释放,如下所述。
所以现在我唯一的问题是单击事件不起作用,并且我没有从 -(void)nextPage{.. 方法获取 NSLog 输出。
这是我创建的测试:
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextBtn setFrame:CGRectMake(0, 100, 80,40)];
[nextBtn setTitle:@"Button Title" forState:UIControlStateNormal];
[nextBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:10]];
[nextBtn addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:nextBtn];
但看起来好像 [nextBtn addTarget:self 操作:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];
是不是开火了?
另一个更新:
我在根/我的视图控制器上尝试了以下代码。
视图控制器.h - (void)nextPage: (id)发件人;
viewController.m
-(void)nextPage: (id)sender
{
NSLog(@"Next Page Clicked");
}
- (void)viewDidLoad
{
CGRect buttonFrame = CGRectMake( 10, 280, 100, 30 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: @"My Button" forState: UIControlStateNormal];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[self.view addSubview: button];
[button addTarget: self
action: @selector(nextPage:)
forControlEvents: UIControlEventTouchDown];
.....
当我尝试这个时,单击的方法起作用了。
所以这一定与我的自定义类视图和按钮的设置有关,就好像它无法识别它一样?
谢谢斯
I am creating a custom view that will act like a footer bar so I can pass in several views rather than using IB to add etc saving time and to learn more coding.
My class first creates the background, then I add a UIButton to this, but for some reason the text is not showing and also the click is not firing.
Custom class h file:
@interface menuViewone : UIView {
IBOutlet UIView *menuback;
}
-(void)nextPage;
-(void)menubackground;
-(void)nextButton;
My custom class m file:
-(void)nextPage{
NSLog(@"Next Page Clicked");
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)menubackground{
NSLog(@"Menu Background Loaded");
menuback = [[UIView alloc] initWithFrame:CGRectMake(0, 410, 320, 50)];
[menuback setBackgroundColor:[UIColor yellowColor]];
[self addSubview:menuback];
[menuback release];
[self nextButton];
}
-(void)nextButton{
NSLog(@"Create Next Button");
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
nextBtn.frame = CGRectMake(0, 410, 60, 50);
[nextBtn setBackgroundColor:[UIColor redColor]];
nextBtn.titleLabel.text = @"next";
nextBtn.titleLabel.textColor = [UIColor blackColor];
[nextBtn setTitle:@"Next" forState:UIControlStateNormal];
[nextBtn addTarget:self action:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:nextBtn];
}
I am calling it in my main m file using:
#import "menuViewone.h"
.......
- (void)viewDidLoad
{
menuViewone *menu = [[menuViewone alloc] init];
[menu menubackground];
[self.view addSubview:menu];
[menu release];
[super viewDidLoad];
}
So at the moment I get a yellow footer background with a red button to the left, but no action or label
1: whats going on?
2: am I doing this right?
UPDATE
I have solved the issue of the text not showing which was when I was releasing the button which would auto release as mentioned below.
So now my only issue is that the click event is not working, and I do not get my NSLog output from the -(void)nextPage{.. method.
Here is the test one I created:
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextBtn setFrame:CGRectMake(0, 100, 80,40)];
[nextBtn setTitle:@"Button Title" forState:UIControlStateNormal];
[nextBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:10]];
[nextBtn addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:nextBtn];
but it seems as if the
[nextBtn addTarget:self action:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];
is not firing?
ANOTHER UPDATE:
I have tried the following piece of code on the root/ my view Controller.
viewController.h
- (void)nextPage: (id)sender;
viewController.m
-(void)nextPage: (id)sender
{
NSLog(@"Next Page Clicked");
}
- (void)viewDidLoad
{
CGRect buttonFrame = CGRectMake( 10, 280, 100, 30 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: @"My Button" forState: UIControlStateNormal];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[self.view addSubview: button];
[button addTarget: self
action: @selector(nextPage:)
forControlEvents: UIControlEventTouchDown];
.....
And when I tried this the clicked method worked.
So it must be something to do with my custom class view and the setting of my button as if its not recognised it??
Thanks Si
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NextPage 不会被调用,因为您使用了不正确的选择器签名。 in:
nextPage 后面的冒号表示“一个名为 nextPage 的方法,只有一个参数。类似于
nextPage:(int)pageNumber {}
,其中有nextPage {}
。如果您将该行更改为:您的操作将被调用。
我不太确定,但由于按钮样式和状态的组合,它可能不会显示您绝对应该将
UIButtonTypeCustom
替换为。UIButtonTypeRoundedRect
如果这不起作用,请确保按钮的状态与您为其设置文本的状态匹配。您可以设置按钮的状态,也可以设置更多状态的文本。后者更可取)。NextPage does not get called because you use the incorrect signature for the selector. in:
the colon after nextPage means "a method called nextPage with a single argument. Something like
nextPage:(int)pageNumber {}
where you havenextPage {}
. If you change that line to:Your action will be called.
The label I'm less sure of but it is probably not displaying because of the combination of button style and state. You should definitely replace
UIButtonTypeCustom
withUIButtonTypeRoundedRect
. If that doesn't work make sure that the state of the button matches the state you set text for. You can either set the state of the button or, set the text for more states (with the latter being preferable).首先,您正在使用类方法创建一个按钮,在这种情况下您不应该释放它。该类方法返回一个自动释放的对象。
其次是下一个代码中的内存泄漏:
您应该释放 *menu 变量,因为它是本地变量,并且当您将其添加为子视图时它会被保留。
所以首先纠正你的内存泄漏,然后我们再讨论。
更新:为了添加先生不傲慢的答案,我会选择 UIControlEventTouchDown 而不是 UIControlEventTouchUpInside。 UIControlEventTouchUpInside 事件非常奇怪,有时它不会向您的处理程序发送通知。
First of all, you are creating a button with a class method, in this case you should not release it. The class method returns an autoreleased object.
Second is a memory leak in your next code:
You should release *menu variable as it is a local one and it gets a retain when you add it as a subview.
So first correct your memory leaks, after that we will talk.
Update: And to add to mister not arrogant answer, I would choose UIControlEventTouchDown instead of UIControlEventTouchUpInside. UIControlEventTouchUpInside event is very strange, some times it does not send notifications to your handlers.