2012-08-30 58 views
2

我在UIScrollViewStoryBoard編程方式添加的按鈕越來越崩潰的故事板

下面添加一個按鈕是我使用的代碼。

-(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方法我已經添加的按鈕和這是越來越墜毀其click事件。

-(void)btnSubImageClicked 
{ 
    NSLog(@"btnSubImageClicked"); 
} 

我遇到的情況是

MyMainViewControllersourceViewController爲我創造customSegue這是UIStoryboardSegue

MyMainViewController有一個UIView爲PlaceHolderView在我加入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]]; 

} 

UPDATE

有了答案,我也不得不改變線路 srcObj.currentViewController = dstObj;

srcObj.addChildViewController = dstObj;

,使其工作

+0

崩潰時控制檯中的錯誤消息是什麼? –

+0

你能看到模擬器中的按鈕嗎? – Krunal

+0

*** - [MyViewController performSelector:withObject:withObject:]:發送到解除分配實例的消息0x6a7f2e0 – Heena

回答

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參數零調用它調用此方法。例如[self btnSubImageClicked:nil];

1

你的操作需要,即使接受了(id) sender說法,您沒有使用它:

-(void)btnSubImageClicked:(id) sender 
{ 
    NSLog(@"btnSubImageClicked"); 
} 

addSubScrollView

[aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside]; 
+0

我有發件人的原始方法,但那是因爲測試目的而崩潰,所以我沒有發件人的方法。在這兩種情況下它都崩潰了 – Heena

1

按鈕目標想

-(void) btnSubImageClicked:(id)sender{} 

嘗試。

更新: -

請更正代碼,更改此行

[aButton addTarget:self action:@selector(btnSubImageClicked:) forControlEvents:UIControlEventTouchUpInside]; 

現在的工作我檢查。

+0

我試過這個,但得到崩潰。 – Heena

+0

請檢查更新的代碼 – Sandy

0

而不是

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

寫這篇文章,

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

和更好的做法總是寫這篇文章,

-(void) btnSubImageClicked:(id)sender{} 
相關問題