Iphone 在这种情况下,我应该使用全局布尔变量吗?
我有一个viewcontroller(比如a)。我将一些viewcontroller推到第一个viewcontroller(A)上。完成一些任务后,我将切换回我的第一个视图控制器(A)。也就是说,我弹出到rootviewcontroller(A)。但是这次我的viewcontroller(A)应该有一个alertviewIphone 在这种情况下,我应该使用全局布尔变量吗?,iphone,ios,uiviewcontroller,global-variables,uialertview,Iphone,Ios,Uiviewcontroller,Global Variables,Uialertview,我有一个viewcontroller(比如a)。我将一些viewcontroller推到第一个viewcontroller(A)上。完成一些任务后,我将切换回我的第一个视图控制器(A)。也就是说,我弹出到rootviewcontroller(A)。但是这次我的viewcontroller(A)应该有一个alertview 我的问题是:在这种情况下,设置全局布尔变量是否正确。我的意思是,我声明了一个全局布尔变量,只有当ViewController弹出时,它才会设置为true。有没有更好的方法可以这
我的问题是:在这种情况下,设置全局布尔变量是否正确。我的意思是,我声明了一个全局布尔变量,只有当ViewController弹出时,它才会设置为true。有没有更好的方法可以这样做。您也可以将其保存在
NSUserDefaults
中。
在ViewController中:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:@"some_key"];
在另一个视图中,视图中的控制器将出现
,例如:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
BOOL val = [prefs boolForKey:@"some_key"];
您还可以将其保存在
NSUserDefaults
中。
在ViewController中:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:@"some_key"];
在另一个视图中,视图中的控制器将出现
,例如:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
BOOL val = [prefs boolForKey:@"some_key"];
您还可以将第一个视图控制器设置为全局变量,例如在AppDelegate中,然后通过
(YourAppDelegate*)[UIApplication sharedApplication]。firstViewController
然后在弹出后调用DisplayAlert函数
或者,您可以将第一个视图控制器作为“主视图控制器”传递给所有后续推送的视图控制器,然后调用该函数,而不需要全局变量
或者,您可以通过调用访问根视图控制器
[self.navigationController.viewControllers objectAtIndex:0]
然后对其调用警报功能。您还可以将第一个视图控制器设置为全局变量,例如在AppDelegate中,然后通过
(YourAppDelegate*)[UIApplication sharedApplication]。firstViewController
然后在弹出后调用DisplayAlert函数
或者,您可以将第一个视图控制器作为“主视图控制器”传递给所有后续推送的视图控制器,然后调用该函数,而不需要全局变量
或者,您可以通过调用访问根视图控制器
[self.navigationController.viewControllers objectAtIndex:0]
然后调用其上的警报功能。确定在谈到ViewController时,您可以使用全局NSString(或BOOL)来显示警报视图
在下面的代码中,我使用了字符串变量。
在AppDelegate.h类中声明NSString变量
NSString * checkAlert;
//make property of that NSString.
@property(retain,nonatomic)NSString * checkAlert;
在AppDelegate.m中
//synthesize checkAlert
@synthesize checkAlert;
checkAlert=@"NotNeed";
然后在ViewController中
-(void)ViewWillAppear{
// here check if checkAlert contains string as you want
if(checkAlert isEqualToString:@"showAlert"){
//here show the AlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"AlerViewmessage" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
//when you abou to navigate to another ViewController the Access that checkAlert String as
-(void)goToAnotheViewController{
AppDelegate* appdele=(AppDelegate*)[[UIApplication sharedApplication]delegate];
appdele.checkString=@"showAlert";
//then navigate to viewController
[self.navigationController pushViewController: animated:YES];
}
//you just need to compare checkString's value string .
它将起作用当然,当涉及到ViewController时,您可以使用全局NSString(或BOOL)来显示AlertView
在下面的代码中,我使用了字符串变量。
在AppDelegate.h类中声明NSString变量
NSString * checkAlert;
//make property of that NSString.
@property(retain,nonatomic)NSString * checkAlert;
在AppDelegate.m中
//synthesize checkAlert
@synthesize checkAlert;
checkAlert=@"NotNeed";
然后在ViewController中
-(void)ViewWillAppear{
// here check if checkAlert contains string as you want
if(checkAlert isEqualToString:@"showAlert"){
//here show the AlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"AlerViewmessage" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
//when you abou to navigate to another ViewController the Access that checkAlert String as
-(void)goToAnotheViewController{
AppDelegate* appdele=(AppDelegate*)[[UIApplication sharedApplication]delegate];
appdele.checkString=@"showAlert";
//then navigate to viewController
[self.navigationController pushViewController: animated:YES];
}
//you just need to compare checkString's value string .
在这种情况下,我将使用布尔变量。但我不能说这是最好的方法。在这种情况下,我将使用布尔变量。但我不能说这是最好的办法。@SAHARA请参考这个@SAHARA请参考这个