Ios 从UIActionSheet类中解除UIViewController
我创建了一个Ios 从UIActionSheet类中解除UIViewController,ios,objective-c,ios7,uiviewcontroller,uiactionsheet,Ios,Objective C,Ios7,Uiviewcontroller,Uiactionsheet,我创建了一个UIActionSheet类,它允许我从应用程序中的任何位置注销。我遇到的问题是,一旦我按下注销按钮,它还应该关闭当前的viewController,并返回登录viewController。下面是我的代码片段 NSObject.h file @interface constants : NSObject<UIActionSheetDelegate>{ UIActionSheet *msg; } -(void)presentMyActionSheet; @
UIActionSheet
类,它允许我从应用程序中的任何位置注销。我遇到的问题是,一旦我按下注销按钮,它还应该关闭当前的viewController
,并返回登录viewController
。下面是我的代码片段
NSObject.h file
@interface constants : NSObject<UIActionSheetDelegate>{
UIActionSheet *msg;
}
-(void)presentMyActionSheet;
@property (nonatomic, strong) UIView *viewForActionSheet;
NSObject.m file
-(void)presentMyActionSheet{
msg = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Log Out" otherButtonTitles:nil, nil];
[msg showInView:self.viewForActionSheet];
}
// actionsheet delegate protocol item
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex{
NSLog(@"button index = %ld", (long)buttonIndex);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(buttonIndex == 0){
[defaults setObject:nil forKey:@"acctInfo"];
[defaults setBool:NO forKey:@"isLoggedOn"];
NSLog(@"You logged out");
它不起作用
}else{
NSLog(@"You canceled logout");
}
}
viewController.h file
@interface viewController : UIViewController<UIActionSheetDelegate>
@property(nonatomic, retain)constants *obj;
viewController.m file
@synthesize obj;
{
self.obj = [[constants alloc] init];
self.obj.viewForActionSheet = self.view;
[self.obj presentMyActionSheet];
if([defaults boolForKey:@"isLoggedOn"]){
If I try it from here it crashes.
// [self.navigationController popViewControllerAnimated:NO];
// [self dismissViewControllerAnimated:NO completion:nil];
}
}
}其他{
NSLog(@“您已取消注销”);
}
}
viewController.h文件
@界面viewController:UIViewController
@属性(非原子,保留)常数*obj;
viewController.m文件
@合成obj;
{
self.obj=[[constants alloc]init];
self.obj.viewForActionSheet=self.view;
[self.obj presentMyActionSheet];
if([默认布尔工作:@“isLoggedOn”]){
如果我在这里尝试,它会崩溃。
//[self.navigationController PopViewControllerInitiated:否];
//[自我解除视图控制器激活:未完成:无];
}
}
关于它为什么不起作用有什么建议吗
谢谢试试这样的方法:
[self.viewForActionSheet.navigationController popToRootViewControllerAnimated:YES];
您应该在对象类中为自己的委托使用NSNotification。您可以使用UIActionSheetDelegate方法
-(void)actionSheet:(UIActionSheet*)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
例如:
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
[self.navigationControllerler dismissModalViewControllerAnimated:YES];}}
你是否尝试过先关闭操作表,然后弹出视图控制器?@pnavk是的,当我尝试应用程序崩溃时。
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
[self.navigationControllerler dismissModalViewControllerAnimated:YES];}}