我在UIScrollView
StoryBoard
添加了一个按钮
以下是我正在使用的代码。
-(void)addScrollView
{
for(int i=0;i<[array count];i++)
{
UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, SUBSCROLLVIEW_WIDTH, SUBSCROLLVIEW_HEIGHT)];
UITextView *txtVwDetail = [[UITextView alloc] initWithFrame:CGRectMake(342, 0, TEXTVIEW_WIDTH, TEXTVIEW_HEIGHT)];
txtVwDetail.text = SAMPLE_STRING;
[self addSubScrollView:subScrollView];
[self.view addSubview:subScrollView];
[self.view addSubview:txtVwDetail];
}
}
-(void)addSubScrollView:(UIScrollView *)aSubScrollView
{
for(int i=0;i<[array count];i++)
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(intBtnX, (aSubScrollView.frame.size.height - 80)/2, 50, 80);
[aButton setImage:[UIImage imageNamed:[self.items objectAtIndex:i]] forState:UIControlStateNormal];
**[aButton addTarget:self action:@selector(btnSubImageClicked) forControlEvents:UIControlEventTouchUpInside];**
aSubScrollView.contentSize = CGSizeMake(intScrollViewWidth, aSubScrollView.frame.size.height);
[aSubScrollView addSubview:aButton];
}
}
在addSubScrollView
方法中,我添加了Button及其点击事件,该事件正在崩溃。
-(void)btnSubImageClicked
{
NSLog(@"btnSubImageClicked");
}
我的情景是
MyMainViewController
是我创建的sourceViewController
的{{1}},customSegue
类
UIStoryboardSegue
将UIView设为MyMainViewController
我在其中添加PlaceHolderView
的视图,这是此Scrollview
MydestinationViewContoller
更新
通过回答,我也必须改变这条线
-(void)perform
{
BrowseScreenVC *srcObj = (BrowseScreenVC *)[self sourceViewController];
UIViewController *dstObj = (UIViewController *)[self destinationViewController];
for(UIView *vw in srcObj.viewPlaceHolder.subviews)
{
[vw removeFromSuperview];
}
NSLog(@"dest : %@",dstObj.view);
NSLog(@"destobj :%@",dstObj);
srcObj.currentViewController = dstObj;
[srcObj.viewPlaceHolder addSubview:[dstObj.view retain]];
}
到
srcObj.currentViewController = dstObj;
使其正常运作
答案 0 :(得分:4)
你必须按以下方式添加目标:
[aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];
@selector()
,在这些大括号中,您必须提供要执行的方法。 “:”表示该方法有一些参数。在这种情况下,参数将是调用方法的按钮本身。
你必须实现你的方法,然后它的签名看起来像这样
- (void)btnSubImageClicked:(id)sender{
// sender would be the button that is calling the method. you can use this object according to your requirements if you want.
// your code
}
如果你想从其他地方调用这个方法,你可以通过传递sender参数nil来调用它。例如[self btnSubImageClicked:nil];
答案 1 :(得分:1)
您的行动需要接受(id) sender
参数,即使您不使用它:
-(void)btnSubImageClicked:(id) sender
{
NSLog(@"btnSubImageClicked");
}
在addSubScrollView
:
[aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];
答案 2 :(得分:1)
按钮目标应该是
-(void) btnSubImageClicked:(id)sender{}
试试这个。
更新: -
请更正您的代码,更改此行
[aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];
现在正在检查工作。
答案 3 :(得分:0)
而不是
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
写这个,
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
为了更好的练习,总是写下来,
-(void) btnSubImageClicked:(id)sender{}