gpt4 book ai didi

ios - 从 UIActionSheet 类中关闭 UIViewController

转载 作者:行者123 更新时间:2023-11-29 10:39:21 27 4
gpt4 key购买 nike

我创建了一个 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 或您自己的委托(delegate)。

关于ios - 从 UIActionSheet 类中关闭 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25266631/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com