2014-10-09 29 views
0

我已經爲ToastView(類似於Android的麪包)創建了一個iOS類,它在屏幕底部顯示一個消息欄。我向ToastView添加了一個按鈕,並解決了我原來的錯誤。用於觸摸按鈕的NSLog顯示在控制檯中,但是當點擊按鈕時我需要將BOOL值發送回HomeViewController,並且我不確定如何使用此按鈕在兩者之間進行通信。任何幫助?iOS中的Android「Toast」:將按鈕添加到消息

在ToastView.h

+ (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration; 
+ (void)undoButtonClicked:(id)sender; 

在ToastView.m:

//shows the "toast" message 
+ (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration { 
//code to show message 
UIButton *undoButton = [[UIButton alloc] initWithFrame:CGRectMake(toast.frame.size.width - 60.0, toast.frame.size.height - 35.0, 40, 20)]; 
undoButton.backgroundColor = [UIColor lightGrayColor]; 
[undoButton addTarget:self action:@selector(undoButtonClicked:) forControlEvents:UIControlEventTouchDown]; 
} 

+(void) undoButtonClicked:(id)sender { 
NSLog(@"UNDO BUTTON TOUCHED"); 
} 

和我在HomeViewController調用ToastView用下面的代碼:

[ToastView showToastInParentView:self.view withText:@"TOAST MESSAGE!" withDuaration:5.0]; 
+0

貌似undoButtonClicked被定義爲一個對象的方法,但你可能會調用它直接在課堂上。你在任何地方使用* [ToastView undoButtonClicked:someParameter]; *? – rdurand 2014-10-09 15:50:36

+0

如果是這樣,讓我知道如果將它改爲* [self undoButtonClicked:someParameter]; *效果更好,所以我可以寫一個完整的答案 – rdurand 2014-10-09 15:55:32

+0

我不知道。我沒有發送任何參數到undoButtonClicked :,我只需要知道按鈕被觸摸的時間。我需要在應用程序中調用此位置? – 2014-10-09 15:57:03

回答

1

看到您的更新代碼,問題來自undoButtonClicked:方法,在您的情況下,它是一個類方法(前綴爲「+」)。

您想通過更改前綴「 - 」使其成爲對象方法。這樣,類就知道它是在這個類的實例化對象上調用的。當您使用addTarget:action:forControlEvents:時,該動作是您目標的選擇器。

作爲蘋果says

選擇器是用於選擇的方法來執行的對象

所以它需要一個對象的方法,而不是一個類方法的名稱。

你的代碼應該看起來像:

.H:

+ (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration; 
- (void)undoButtonClicked:(id)sender; 

.M:

//shows the "toast" message 
+ (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration { 
    //code to show message 
    UIButton *undoButton = [[UIButton alloc] initWithFrame:CGRectMake(toast.frame.size.width - 60.0, toast.frame.size.height - 35.0, 40, 20)]; 
    undoButton.backgroundColor = [UIColor lightGrayColor]; 
    [undoButton addTarget:self action:@selector(undoButtonClicked:) forControlEvents:UIControlEventTouchDown]; 
} 

-(void) undoButtonClicked:(id)sender { 
    NSLog(@"UNDO BUTTON TOUCHED"); 
} 
+0

感謝您的答案,那麼我怎麼能夠從undoButtonClicked返回一個值:我調用showToastInParentView的任何視圖控制器:從 – 2014-10-13 14:27:14

+0

@MSU_Bulldog:沒有直接簡單的答案。我建議你看看協議和代表。我會把這個添加到我的答案中,並讓你知道。 – rdurand 2014-10-13 15:46:49

+0

@MSU_Bulldog:終於我不會將這添加到我的答案,因爲它是一個完全不同的問題。你應該清理你的代碼併爲這個特定的請求打開一個新的問題。 – rdurand 2014-10-13 16:05:05

1

你的類可能看起來像這樣才能按照你的願望工作:

@interface ToastView : UIView 
- (void)undoButtonClicked:(id)sender; 
@end 

@interface NotToastView : UIView 
@property (weak) ToastView *toastView; 
@end 

@implementation NotToastView 
- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
    UIButton *undoButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [undoButton setFrame:self.bounds]; 
    [undoButton addTarget:self action:@selector(undoButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:undoButton]; 
    } 
    return self; 
} 

- (void)undoButtonClicked:(id)sender { 
    [self.toastView undoButtonClicked:sender]; 
} 
@end 

你最好在這裏使用委託協議,而不是要求特定的類。上面的代碼沒有明確地將值設置爲toastView屬性。預計您將在某個地方以UIViewController的身份構建這些內容,並且將自己設置該值,而不是依靠NotToastView來設置它自己的ToastView