2013-03-21 23 views
4

我做了一個簡單的應用程序中,我在每個方向上加載不同.xib點擊的UIButton的事件不會被炒魷魚

我有兩個廈門國際銀行的PortxibLandxib,在這兩個xibs,我拖着一個按鈕和分配

在兩個xib中標記值爲1

和我在viewDidLoad

UIButton *btn=(UIButton *)[self.view viewWithTag:1]; 
    [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside]; 

這裏是選擇方法寫了這個代碼,

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

的UIButton的Click事件不會被炒魷魚......

OR

有沒有其他方法可以調用與b相同的按鈕方法其方向?

編輯:

.h file

.m file

+0

您可以嘗試添加willAnimateRotationToInterfaceOrientation中的代碼以激發兩個方向 – 2013-03-21 12:14:23

+0

您可以嘗試我的答案,從兩個XIB中調用相同的方法。 – Vedchi 2013-03-21 12:54:23

+0

是你的按鈕點擊事件發射任何一個方向?無論是在風景或肖像? – 2013-03-21 14:17:44

回答

6

確保...

  1. 按鈕被放置在根視圖中xib因爲你發現它在self.view或者確保它被放置在正確的觀點,並使用該視圖與viewWithTag
  2. 已分配標籤,所以才讓確定沒有其他控制01​​相同的觀點有標籤1其他明智的按鈕不會被標籤識別。如果這將是你的情況,那麼只需將按鈕的標籤更改爲該視圖的非常不尋常的數字,如999或其他任何東西並進行測試。

編輯

我的感覺就像你在觀看時viewDidLoad沒有得到認可,你必須設置你的代碼。因此,只需在viewDidLoad中註釋該代碼,並在加載nib之後將其更改爲changeOrientation方法。

-(void)changeOrientation 
{ 
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 


    if(orientation==UIInterfaceOrientationPortrait || orientation==UIInterfaceOrientationPortraitUpsideDown) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"PortraitXib" owner:self options:nil]; 
    } 
    else 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"LandscapeXib" owner:self options:nil]; 
    } 

    //Adding this code here makes sense that nib is loaded and after that we are recognizing button from loaded view of nib. 
    UIButton *btn = (UIButton*)[self.view viewWithTag:1]; 
    [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];  
} 

希望這會有所幫助。

+0

我創建了沒有xib的類,並添加了兩個xib一個肖像和其他的景觀,在這兩個xib中都有一個帶有相同標籤的按鈕..但是仍然點擊事件並沒有被解僱.. – Krunal 2013-03-21 12:23:53

+0

只要求你確保沒有任何其他控件在xib上,標籤1除了你的按鈕 – 2013-03-21 12:27:23

+0

是的,只有按鈕有標記1 – Krunal 2013-03-21 12:31:16

5

在頭文件中添加

IBOutlet UIButton *btn; 

參考從這個BTN您xib文件。

在這裏,你的情況,你所使用的BTN所以這種方法在不觸發

-(IBAction)BtnClick:(id)sender 
{ 
    NSLog(@"BtnClick"); 
} 

不會從廈門國際銀行文件中引用使功能IBAction爲從筆尖BTN參考這一點。 在這種情況下,你不需要顯式地添加目標。

如果您不添加在筆尖的按鈕,然後不要忘了初始化和addsubview到視圖

你可以看到這個教程http://www.idev101.com/learn/interface_builder_connections.html

編輯檢查什麼是印刷?

 for (UIButton *but in [self.view subviews]) 
    { 
      NSLog("but.tag %d",but.tag); 
      if(but.tag==1)// compare for same row elements 
      { 
      NSLog("Button Added"); 

       [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside]; 
      } 
     } 
+0

日誌顯示btn.tag是'1'並顯示按鈕添加,但仍然點擊事件沒有被解僱.. :( – Krunal 2013-03-21 13:20:40

0

您可以添加一個這樣的按鈕。 將此添加到你的地方didDoad方法

UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [aButton setTag:1]; 
    [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [aView addSubview:aButton]; 
} 

// then ... 

- (void)buttonClicked:(UIButton*)button 
{ 
    NSLog(@"Button %d clicked.", [button tag]); 
}