UIAlertView问题

时间:2010-09-08 20:16:02

标签: iphone objective-c uinavigationcontroller uialertview

我有一点怀疑。我有一个NSObject类,我试图显示一个警报视图。因此,当我点击OK按钮后显示警报视图时,我想将导航控制器推到堆栈上。我可以从一般的NSObject类推送导航控制器吗?请让我知道伙计们......谢谢你的时间..

这是代码..

- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{    
  SettingsViewController *homeView = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
    [self.navigationController pushViewController:homeView animated:NO];
    [homeView release];

}

我正在创建一个名为navigationController的UINavigationController类型的属性,当我发现错误时,我正在显示一个警报视图,我正在使用上面的方法来推送视图控制器,但它不起作用..

1 个答案:

答案 0 :(得分:3)

是和否......取决于您如何设置应用程序。要将视图推送到导航堆栈,您需要有一个导航控制器

您的NSObject是否可以访问此导航控制器 - 您可能必须设置一个委托方法,当您在NSObject中调用警报视图委托时,该方法将从您的委托视图中调用。

我只是想知道你为什么在NSObject中显示UIAlertView,为什么不在UIView或UIViewController中显示它?

CustomObject.h

@protocol CustomObjectDelegate<NSObject>
@optional
- (void)customObjectAlertViewDidClickOk;
@end

@interface CustomObject : NSObject <UIAlertViewDelegate>{
    id<CustomObjectDelegate> delegate;
}
@property (nonatomic, assign) id<CustomObjectDelegate> delegate;
@end;

CustomObject.m

@synthesize delegate;
// then put this:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    [delegate customObjectAlertViewDidClickOk];
}

然后,您的ViewController .h文件需要包含自定义对象并分配委托方法:

#include "CustomObject.h"
@interface MyViewController : UIViewController <CustomObjectDelegate> {

}
@end

和.m viewDidLoad(或类似):

- (void)viewDidLoad{
    CustomObject *obj = [[CustomObject alloc] init];
    [obj setDelegate:self];
}
- (void)customObjectAlertViewDidClickOk{
    AnotherViewController *page = [[AnotherViewController alloc] initWithNibName:nil bundles:nil];
    [self.navigationController pushViewController:page];
}

这就是我会怎么做的 - 因为我不太确定我完全明白你在问什么。 :)这也是我头脑中的所有 - 所以不要把它写成信,但你有基础从那开始。你可以建立它。查找@protocols并委托方法,其全部在那里。 :)