【发布时间】:2012-10-13 13:37:00
【问题描述】:
我有一个单独的 UIView 类,它构造一个包含 UIButton 的简单页脚栏。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:CGRectMake(0, 410, 320, 50)];
if (self) {
int buttonHeight = self.frame.size.height;
int buttonWidth = 70;
int nextBtnPosX =0;
int nextBtnPosY =0;
self.backgroundColor =[UIColor colorWithRed:254.0/255.0 green:193.0/255.0 blue:32.0/255.0 alpha:1.0];
[self sendSubviewToBack:self];
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextBtn setTitle:@"Next" forState:UIControlStateNormal];
nextBtn.frame = CGRectMake(nextBtnPosX, nextBtnPosY, buttonWidth, buttonHeight);
[nextBtn addTarget:self.superview action:@selector(GoToNextPage) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:nextBtn];
}
return self;
}
我有几个 ViewController,然后将上面的页脚视图类作为子视图添加到每个视图中。
UIView *newFooter = [[theFooter alloc] init];
[self.view addSubview:newFooter];
现在,页脚视图中的实际 UIButton 需要为其添加的每个 viewController 设置不同的标签。
所以我认为最好将 IBAction 添加到实际的视图控制器,然后通过页脚视图调用它。
但这就是我遇到问题的地方。如何从 addTarget 的页脚子视图中调用父控制器来初始化 IBAction(GoToNextPage)?
将它全部放在页脚子视图中并传入所需的目标是否会更容易,如果是这样,该怎么做。
【问题讨论】:
-
我已经设法通过在操作部分添加这行代码来防止应用程序表单崩溃 [nextBtn addTarget:self.superview action... 但这可能意味着它到达某个地方但只是不调用我的父视图中的 IBAction...
-
我建议对这种事情使用委托...
-
感谢您的反馈,对此方法有任何建议