2012-12-31 36 views
1

問題:無法從UIButton的獲取UITouch事件

我不能讓我的UIButton送我的一類事件調用

這是我第一次嘗試:

我做了一個類其延伸的UIViewController,稱爲查看器

#import "Viewer.h" 
#import "MainScreen.h" 

@implementation Viewer 

- (void)viewDidLoad 
{ 
    MainScreen* main = [[MainScreen alloc]init: self]; 
    [main show]; 

    [super viewDidLoad]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 

然後:

  • 我創建了一個名爲MainScreen類,2個IBOutlets - singlePlayerButton和視圖
  • Viewer是UIViewController中的一個實例
  • buttonPressed具有IBAction爲
  • 的返回類型

MainScreen:

#import "MainScreen.h" 
#import "Viewer.h" 

@implementation MainScreen 
{ 
    IBOutlet UIButton* singlePlayerButton; 
    IBOutlet UIView* view; 

    Viewer* viewer; 
} 

- (id)init:(Viewer*) theViewer 
{ 
    self = [super init]; 
    viewer = theViewer; 
    return self; 
} 

- (void)show 
{ 
    [[NSBundle mainBundle] loadNibNamed:@"MainScreenLayout" owner:self options:nil]; 
    [viewer.view addSubview:view]; 
} 

- (IBAction)buttonPressed:(id)sender 
{ 
    NSLog(@"A button was pressed!"); 
} 

@end 

然後我創建了一個名爲MainScreenLayout.xib

這是它看起來像E:

Screenshot of my NIB file

正如你所看到的,我有四個按鈕,但連接到任何東西只有一個是單人遊戲按鈕,我也UIView的連接查看。現在

,我掛像這樣的動作單人遊戲按鈕:

Screenshot of connecting the action to the button

buttonPressed是我的方法,我想在按下按鈕時呼籲,所以我CTRL-點擊First Responder,然後點擊buttonPressed旁邊的圓圈,然後將其拖到單人播放按鈕的頂部。然後彈出下一個對話窗口,我點擊Touch up Inside。以下是收稿操作窗口的樣子:

Screenshot of First Responder Window

現在,我想在這一點上它應該工作。我在iPhone模擬器上運行它並按下按鈕。

只是所以沒有往哪裏去的問題或不是我能幹,這裏是我的實際點擊按鈕的圖片:

Screenshot

我沒有任何輸出。然後我去了我的代碼並查看了方法。這是什麼樣子:

Screenshot of method

,你可以看到,橢圓形不填充,這意味着該方法不配對。所以自然我認爲它被另一種方法所覆蓋。因此,這裏有什麼方法貌似現在:

- (IBAction)aVerySpecificMethodNameForMyButtonListener:(id)sender 
{ 
    NSLog(@"A button was pressed!"); 
} 

然後我又回到了我的NIB窗口,不成對我的單人遊戲按鈕,然後將它修好aVerySpecificMethodNameForMyButtonListener。現在,這是什麼樣子:

Screenshot for rediculous method name

然後我又跑了,仍然一無所獲,當我按下按鈕。然後我又回到了代碼:

Screen shot

(此處插入的挫折)

在這一點上,我重新啓動Xcode和模擬器和看着一切再次,它仍然沒有工作。

這是我嘗試下一個,這似乎大約十億倍以上簡單:

我與我的show方法與上面的一個替代。唯一不同的是最後一行。我從NIB中的單人按鈕斷開了我的方法。然後我跑了它,我因爲獲得了一些輸出而興奮了一秒鐘。這裏是我得到的:

(lldb) 

非常有幫助的輸出C調試器,一如既往。我花了接下來的幾個小時,試圖找出我做錯了什麼。我已經嘗試了無數的例子,但都沒有成功。

有沒有人看到我在這裏做錯了?它只是Xcode是flakey?

感謝您的閱讀!歡迎所有相關答案!


UPDATE:

我也嘗試過改變的UIButton的聲明在頭文件的屬性:

#import <Foundation/Foundation.h> 
#import "Viewer.h" 

@interface MainScreen : NSObject 
{ 
    IBOutlet UIView* view; 

    Viewer* viewer; 
} 

@property (weak, nonatomic) IBOutlet UIButton* singlePlayerButton; 

- (id)init:(Viewer*) theViewer; 
- (void)show; 
- (IBAction)aVerySpecificMethodNameForMyButtonListener:(id)sender; 

@end 

因此,這裏是我在MainScreen.m改爲遵守:

#import "MainScreen.h" 
#import "Viewer.h" 

@implementation MainScreen 

- (id)init:(Viewer*) theViewer 
{ 
    self = [super init]; 
    viewer = theViewer; 
    return self; 
} 

@synthesize singlePlayerButton; 

- (void)show 
{ 
    [[NSBundle mainBundle] loadNibNamed:@"MainScreenLayout" owner:self options:nil]; 
    [viewer.view addSubview:view]; 

    [singlePlayerButton addTarget:self action:@selector(aVerySpecificMethodNameForMyButtonListener:) forControlEvents:UIControlEventTouchUpInside]; 
} 

- (IBAction)aVerySpecificMethodNameForMyButtonListener:(id)sender 
{ 
    NSLog(@"A button was pressed!"); 
} 

@end 

現在MainScreen.m文件中沒有變量聲明,我將它們移動了al l到標題。 singlePlayerButtonview旁邊的圓圈仍然填充,但該方法旁邊的氣泡不是。我再次運行代碼並獲得相同的輸出。

+0

在你最後一次嘗試得到這個工作時,你是否在頭文件中聲明瞭'UIButton'屬性?應該像'@property(弱,非原子)IBOutlet UIButton * singlePlayerButton;'。您還需要將按鈕連接到此聲明(填充旁邊的圓圈)。然後勾選動作,需要將該按鈕引用爲'self.singlePlayerButton addTarget ...' – bobnoble

+0

@bobnoble'singlePlayerButton'和'view'旁邊的氣泡都被填充。我的方法旁邊的按鈕沒有被填充。現在,我只是將UIButton定義在'MainScreen'實現的大括號內。我會將其添加爲屬性,然後我會更新我的帖子。好主意。 – John

回答

1

所以我會把它放在註釋中,因爲它真的只是一個建議,但是我沒有代表,所以很抱歉讓你的希望上升,但我相信touch事件的作用域只在它的UIView裏面發生。當touch up事件被觸發時,它不會被傳遞給UIView的處理程序(MainScreen類),因此事件的偵聽器方法不會被調用...但是您可以將它在界面構建器中鏈接到按鈕上對我感到困惑,所以我可能會離開。

無論如何,我認爲你應該去下一步就是讓你的MainScreen成爲一個UIView對象,看看它對視圖中按鈕的事件處理有什麼作用。

+0

謝謝你的迴應,我將重新調整我的程序,看看它是如何工作的。希望它有效! – John