Iphone 以编程方式添加的按钮在情节提要中崩溃
我正在Iphone 以编程方式添加的按钮在情节提要中崩溃,iphone,objective-c,ios,uibutton,storyboard,Iphone,Objective C,Ios,Uibutton,Storyboard,我正在UIScrollView的StoryBoard中添加一个按钮 下面是我正在使用的代码 -(void)addScrollView { for(int i=0;i<[array count];i++) { UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, SUBSCROLLVIEW_WIDTH, SUBSCROLLVIEW_HEIGHT)
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];
}
}
我有一个场景
MyMainViewController
是我创建的customSegue
的sourceViewController
,它是UIStoryboardSegue
类
MyMainViewController
具有作为占位符视图的UIView
,我正在其中添加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代码>
到
srcObj.addChildViewController=dstObj代码>
要使其工作您的操作需要接受一个(id)sender
参数,即使您没有使用它:
-(void)btnSubImageClicked:(id) sender
{
NSLog(@"btnSubImageClicked");
}
在addSubScrollView
中:
[aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];
按钮目标应该是什么样的
-(void) btnSubImageClicked:(id)sender{}
试试这个
更新:-
请更正代码,更改此行
[aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside];
现在正在工作,我检查了。而不是
UIButton*aButton=[UIButton button类型:uibuttonypecustom]代码>
写这个,
UIButton*aButton=[UIButton按钮类型:uiButtontyPeroundRect]代码>
为了更好的练习,一定要写下这个
-(void) btnSubImageClicked:(id)sender{}
您必须按如下方式添加目标:
[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
}
如果您也想从其他地方调用此方法,可以通过传递发送方参数nil来调用它。e、 g[自BtnusBimageClicked:无]代码>崩溃时控制台中的错误消息是什么?您能在模拟器中看到按钮吗?***-[MyViewController性能选择器:withObject:withObject:]:消息发送到解除分配的实例0x6A7F2E0您的“自我”是什么?我希望是viewController。!!我和发送者有一个原始的方法,但它被崩溃了,所以为了测试的目的,我做了一个没有发送者的方法。在这两种情况下,它都被崩溃了
- (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
}