2014-10-09 54 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