在不同地方使用相同的UialertView



我有一个uialertview,arperview,在viewdidload-hodod中初始化,就像:

self.alertView = [[UIAlertView alloc] initWithTitle:@"Meddelande"
                                            message:@"Do you want to continue?"
                                           delegate:self
                                  cancelButtonTitle:@"No"
                                  otherButtonTitles:@"Yes", nil];

我也有许多通过按不同按钮来调用的Metods:

- (IBAction)didPressButton1 {
    [self.alertView show];
}

我认为这将显示AlertView,但什么也不会发生。一种解决方案是调用初始化AlertView并同时显示的METOD,但我希望它能起作用。

AlertView的属性看起来像:

@property (nonatomic, strong) UIAlertView *alertView;

我还在类似的类延伸中添加了合法程序:

@interface MyViewController () <UIAlertViewDelegate>
@end

我在做什么错?

汉克

您没有做任何不好的事情。此代码有效。您的错误是您如何声明alertView

确保您这样做

@property (nonatomic, retain)UIAlertView *alertView;

您将didPressButton1方法连接到UIButton

在接口(.h)文件

@interface Your_Class:NSObject <UIAlertViewDelegate>
@property (strong, nonatomic) UIALertView *uv;

//在实现文件中

@synthesize uv;

//在ViewDidload中

self.uv = [[UIAlertView alloc] initWithTitle:@"Meddelande"
                                            message:@"Do you want to continue?"
                                           delegate:self
                                  cancelButtonTitle:@"No"
                                  otherButtonTitles:@"Yes", nil];

//在按钮操作

- (IBAction)didPressButton1 {
    [self.uv show];
}

您是否检查了您的didpressbutton1方法是在调用吗?如果没有,请以正确的方式连接它。

最新更新