在我的项目中,我想始终访问该位置。如果用户授予访问位置的权限仅在使用时应用程序,我想用一个按钮显示自定义设计的alertView
,然后单击该按钮用户将导航到应用程序设置屏幕以更改位置权限。我想要在许多 ViewControllers
中使用相同的警报 View 。它如何使之成为可能?
我的代码:
- (IBAction)changetoAlwaysClicked:(id)sender {
_locationView.hidden = YES;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
}];
}
最佳答案
我认为这可以通过创建一个 xib 来正确解决,在那个文件中创建你想要的 View (或者在这种情况下你只是移动它)。还要添加一个 ViewController 并将警报所需的逻辑放在那里。
然后在任何你需要它的 View Controller 中,你只需要像这样调用它。
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 的 View 导出以使其工作很重要。
此外,如果您不想在某些 View Controller 中显示某些属性,您可以创建一个枚举并通过传递正确的枚举值来进行适当的更改。
首先定义枚举
typedef NS_ENUM(int, CustomAlertViewControllerMode) {
CustomAlertViewControllerModeExample1 = 0,
CustomAlertViewControllerModeExample2 = 1,
};
在你的头文件中添加属性
@property CustomAlertViewControllerMode mode;
然后当你调用它的时候,你只需在presentViewController方法之前添加这一行
[customVC setMode:CustomAlertViewControllerModeExample1];
然后在您的 CustomViewController 内部根据枚举值隐藏所需的 View 。
关于ios - 设计一个自定义 View ,用于多个 ViewController 的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52071474/