ios - 如何在 Storyboard iOS 7 中正确重用模态视图/ Controller

标签 ios objective-c

所以在我的应用程序中,我在导航 Controller 中嵌入了一个向导类型的流程,非常标准的应用程序设计,并且工作正常。

所以此时我的设计问题是我需要在向导中的每个点引入“日期”和日期选择,之前我只是将所有时间戳记到服务器上的 DateTime.Now()端,但这需要用户现在可以编辑。

我似乎无法理解使用 Storyboard执行此操作的正确方法,我只想在 Storyboard某处放置一个“占位符”Datepicker View 并重新使用它。我不想为向导上的每个页面创建一个 segue 和日期选择器 View ,这就是我现在所做的,这太糟糕了。

所以我试着让它按如下方式工作:

我有一个通用的日期选择器控件,并使用以下代码将其拉出:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
UIViewController *vc = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"GenericDate"];

[self presentViewController:vc animated:YES completion:nil];

这会调出 View ,我可以选择一个日期,但是有一些问题:

  1. 它不是“导航 Controller ”或向导流程的一部分
  2. 我真的不知道如何“响应”日期选择器的结果,即使它已正确嵌入到 nag Controller 中也是如此

如果有人能正确地向我解释如何在 Storyboard流程中重用“组件 View ”,我将不胜感激。

此时 segues 和 View Controller 只有在它们是唯一的情况下才有意义,我不太明白如何使它们成为可重用的组件。

最佳答案

如果你想用 Storyboard来做,那么一个答案是:

  1. 创建一个带有日期选择器的 Controller
  2. 在日期选择器 Controller 中创建委托(delegate)协议(protocol)(将其添加到 XYZDatePicker.h 中的导入下方):

    #import<UIKit/UIKit.h>
    
    @protocol DatePickerDelegate;
    
    @interface XYZDatePicker : UIViewController
    
    @property (nonatomic,weak)id<DatePickerDelegate> delegate;
    
    @end
    
    @protocol DatePickerDelegate<NSObject>
    -(void)datePickerViewController:(XYZDatePicker*)datePicker didPickDate:(NSDate*)date;
    @end
    
  3. 在日期选择器中创建一个属性以设置委托(delegate)协议(protocol)(见上文)

  4. 从需要选择器的每个 View Controller 连接一个模态转场到 日期选择器 Controller
  5. 在每个使用日期选择器的 vc 中实现协议(protocol)。这告诉向您确认协议(protocol)的其他类声明。 (在您调用的 vc .h 文件中导入 DatePicker 并将委托(delegate)添加到接口(interface)行):

    #import XYZDatePicker.h
    
    @interface XYZCallingViewController : UIViewController<XYZDatePickerDelegate> 
    
  6. 在每个调用 vc 的 'prepareForSegue' 方法中,将 vc 设置为日期选择器的委托(delegate)

  7. 在日期选择器中选择日期后,您可以调用委托(delegate)协议(protocol),它会将所选值返回给调用 vc(在下面添加到您的 XYZDatePicker.m,其中“日期”是您想要的日期返回调用 VC):

    if([self.delegate respondsToSelector@selector(datePickerViewController:didPickDate:)] {
        [self.delegate datePickerViewController:self didPickDate:date];
    } 
    
  8. 在您调用的 VC .m 文件中,您需要实现您在 .h 文件中声明的协议(protocol)方法。这是日期值将返回给您的地方:

    -(void)datePickerViewController:(XYZDatePicker*)datePicker didPickDate:(NSDate*)date {
        //dismiss the date picker
        [self dismissViewControllerAnimated:YES completion:^(void) {
             //you may need to wait to call other UI transitions until this completes so you can put them in this block (or can set '...completion:nil];' if you don't need it
        }];
    
        if(date) {
            //do something with the date
        } else {
            //if date=nil then the user cancelled (assuming you use this same delegate method if you allow the user to cancel picking the date)
        }
    }
    

另一种方法是在 Storyboard 中布局日期选择器,然后实例化它并从调用 vc 中呈现它。您仍然需要委托(delegate)协议(protocol)来返回选定的值。这消除了转场(第 4 步和第 6 步)。

引用资料/教程链接:

关于ios - 如何在 Storyboard iOS 7 中正确重用模态视图/ Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25006408/

相关文章:

ios - 在运行时向 UITableView 添加更多项目

ios - Objective-C 中的字符串比较与 isEqualToString

iOS 自定义 UIView 在 iphone xr 和 iphone 7 中具有不同的布局

iphone - 尝试(&失败)从 iPhone 向 sqlite 中插入数据

objective-c - 如何在 watchOS 2 中获取当前位置?

ios - 将 App Delegate 从 Objective-C 换成 Swift?

iphone - 核心数据 : Creating Relationships

javascript - 如何使用 Node.js 标记化 Markdown ?

ios - 适用于 iOS 应用程序的 QuickBlox

iOS 硬件支持的 key 认证