【问题标题】:Dismissing a UIViewController from a UIActionSheet Class从 UIActionSheet 类中关闭 UIViewController
【发布时间】:2014-10-05 15:50:26
【问题描述】:

我创建了一个UIActionSheet 类,它允许我从应用程序中的任何位置注销。我遇到的问题是,一旦我点击注销按钮,它也应该关闭当前的viewController 并返回登录viewController。下面是我的代码的 sn-p。

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

    }
}

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

谢谢

【问题讨论】:

  • 您是否尝试过先关闭操作表然后弹出视图控制器?
  • @pnavk 是的,当我尝试应用程序崩溃时。

标签: ios objective-c ios7 uiviewcontroller uiactionsheet


【解决方案1】:

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

例如:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      试试这样的:

      [self.viewForActionSheet.navigationController popToRootViewControllerAnimated:YES];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多