ios - 设计一个自定义 View ,用于多个 ViewController 的

标签 ios objective-c

在我的项目中,我想始终访问该位置。如果用户授予访问位置的权限仅在使用时应用程序,我想用一个按钮显示自定义设计的alertView,然后单击该按钮用户将导航到应用程序设置屏幕以更改位置权限。我想要在许多 ViewControllers 中使用相同的警报 View 。它如何使之成为可能?

我的代码:

- (IBAction)changetoAlwaysClicked:(id)sender {
    _locationView.hidden = YES;
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
    }];
}

enter image description here 非常感谢任何帮助..!

最佳答案

我认为这可以通过创建一个 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/

相关文章:

ios - react native 文本输入安全输入字体大小轻弹

ios - 健全性检查 : Architecture for designing views that work in both iOS and OSX?

ios - 样式化现有的 UITextField

ios - 成功登录StackMob后如何将 "push"设置为新的 View Controller

ios - 在 obj-C 中调用转换对象的方法

ios - 什么是 __NSCFData 以及它与 NSData 有何关系?

ios - iTunes 连接无法上传大应用程序图标

ios - 对 Alert 按钮使用标准的 Apple 翻译?

ios - 如何通过 UIDocumentInteractionController 共享多个文件?

iphone - 如何在 iPhone 的 TableCell 中获取 TextField 的位置?