从UIActionSheet类中解除UIViewController

时间:2014-08-12 14:15:06

标签: ios objective-c ios7 uiviewcontroller uiactionsheet

我创建了一个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");

这就是我的尝试。

   viewController *controller = [[viewController alloc]init];
   [controller.navigationController popViewControllerAnimated:NO];  
   [controller dismissViewControllerAnimated:NO completion:nil];

它不起作用。

            }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];

    }
}

有关为什么不起作用的任何建议?

由于

3 个答案:

答案 0 :(得分:2)

您应该在对象类中使用自己的委托上的NSNotification。

答案 1 :(得分:0)

尝试这样的事情:

[self.viewForActionSheet.navigationController popToRootViewControllerAnimated:YES];

答案 2 :(得分:0)

您可以使用UIActionSheetDelegate方法-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

例如:

 -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {
        [self.navigationControllerler dismissModalViewControllerAnimated:YES];}}