簡體   English   中英

從UIActionSheet類中消除UIViewController

[英]Dismissing a UIViewController from a UIActionSheet Class

我創建了一個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];

    }
}

關於它為什么不起作用的任何建議?

謝謝

您應該在對象類中使用NSNotification或自己的委托。

嘗試這樣的事情:

[self.viewForActionSheet.navigationController popToRootViewControllerAnimated:YES];

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

例如:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM