如果它是用户第一次使用我的应用程序,我正在尝试打开一个特殊的视图控制器。我之前在其他情况下已经让它工作过,但由于某种原因它现在不起作用。我尝试了两种不同的方法,但没有一种有效。我尝试的第一件事是直接从应用程序代表检查并进行操作......然后我尝试检查这是否是第一次从根视图控制器 viewDidLoad 方法,以及是否是第一次调用 seque 打开特殊的注册页面。尼特正在工作,我不知道为什么.....这是我正在使用的一些代码:
这是在根视图控制器中。当我这样做时,它会在日志上打印出"Main is First Time",所以我知道它正在那里。我仔细检查了故事板中的 segue,它的名称正确,并且往返于正确的地方......
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if([appDelegate firstTime])
{
[self performSegueWithIdentifier:@"mToRegister" sender:self];
NSLog(@"Main is First time");
}
// Do any additional setup after loading the view.
}
在应用委托内部:
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
BOOL haveused = [standardUserDefaults boolForKey:@"haveused"];
if(haveused){
//NOT THEIR FIRST TIME
//Handle scenario
firstTime = NO;
NSString* guid = [[NSMutableString alloc]initWithString:[self getMacAddress]];
//interest = [appDelegate interest];
NSString *splitter = [[NSString alloc]initWithString:@"&"];
NSMutableString *post = [[NSMutableString alloc]initWithString: @"user_read=1&guid="];
[post appendString:guid];
NSLog(@"String for Post is %@", post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setValue:@"Basic ==" forHTTPHeaderField:@"Authorization"];
[request setURL:[NSURL URLWithString:@"http://something.com/app/usercrud.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
theConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
else {
NSLog(@"This is the First time");
firstTime = YES;
//THEIR FIRST TIME
//Handle scenario
// UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
// bundle: nil];
//
// UIViewController *controller = (UIViewController*)[mainStoryboard
// instantiateViewControllerWithIdentifier: @"register"];
// UIViewController *root = (UIViewController *)self.window.rootViewController;
// [root presentModalViewController:controller animated:YES];
[self.window.rootViewController performSegueWithIdentifier:@"mToRegister" sender:self];
}
任何想法为什么它不起作用?如果没有人知道处理这种情况的更好方法?
我认为您的应用程序委托没有得到妥善管理,请使用 NSDefaults 存储任何唯一的标识并在下次启动时检查,如果 NSDefaults 中存在,那么这不是第一次,如果不存在,则不是第一次。 测试是否仍然存在 PRBLM,如果仍然存在,那么也从应用程序委托中发布您的 if 部分
你用的是什么样的 segue?模态?我相信推送需要导航控制器这就是我问的原因。
您在 RootViewController 的 viewDidLoad 中使用的代码应该像我在希望用户登录到我的应用程序时执行类似操作一样工作。所以我唯一的问题与 segue 是如何设置的,并确保它是有效的等等。
也许尝试删除 segue 并重新创建它?
同样偏离了问题的主题,但最佳做法是不要在 AppDelegate 本身中放置大量代码,也许最好将其重构为单独的对象/单例。