在我的项目中,我想始终访问该位置。如果用户将访问位置的权限设置为“仅在使用应用程序时”,我想用一个按钮显示一个自定义设计的alertView
,然后单击该按钮,用户将导航到应用程序设置屏幕以更改位置权限。我希望在许多ViewControllers
中使用相同的警报视图。如何使其成为可能?
我的代码:
- (IBAction)changetoAlwaysClicked:(id)sender {
_locationView.hidden = YES;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
}];
}
任何帮助深表感谢..!
最佳答案
我认为可以通过创建xib来正确解决此问题,在该文件中创建所需的视图(或者在这种情况下,只需移动它即可)。还要添加一个ViewController并在其中放置警报所需的逻辑。
然后,在您需要的任何视图控制器中,只需要这样调用即可。
CustomAlertViewController *customVC = [[CustomAlertViewController alloc] initWithNibName:'CustomAlertViewControllerNAME' bundle:nil];
customVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
customVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; //or any other animation that you want
[self presentViewController:customVC animated:YES completion:nil];
重要的是设置CustomViewController的视图出口以完成此工作。
同样,如果您不想在某些视图控制器中显示某些属性,则可以通过仅传递正确的枚举值来创建枚举并进行适当的更改。
首先定义枚举
typedef NS_ENUM(int, CustomAlertViewControllerMode) {
CustomAlertViewControllerModeExample1 = 0,
CustomAlertViewControllerModeExample2 = 1,
};
在头文件中添加属性
@property CustomAlertViewControllerMode mode;
然后当您调用它时,只需在presentViewController方法之前添加此行
[customVC setMode:CustomAlertViewControllerModeExample1];
然后,如果需要,则在您的CustomViewController内部隐藏所需的视图(基于枚举值)。
关于ios - 设计一个自定义 View ,用于许多ViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52071474/