如何实现模态日期选择器?

2024-03-20

我正在使用埃德·马蒂回答这个问题的代码here https://stackoverflow.com/questions/1624866/iphone-sdk-how-to-implement-a-modal-date-picker但我在一些方面遇到了真正的麻烦。

单击按钮后,我出现了日期选择器,但“完成”按钮却没有出现。我还从该行收到错误:

[delegate datePickerController:controller didPickDate:datePicker.date];

错误信息:

“控制器”未声明(在此函数中首次使用)

我总共有 6 个文件:

  • ModalDatePickerViewController.m
  • 模态日期选择器ViewController.h
  • ModalDatePickerAppDelegate.m
  • 模态 DatePicker AppDelegate.h
  • DatePickerController.m
  • 日期选择器控制器.h

我的 DatePickerController.h 看起来像:

    #import <UIKit/UIKit.h>


@class DatePickerController;
@protocol DatePickerControllerDelegate
- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date;
@end

@interface DatePickerController : UIViewController {
    UIDatePicker *datePicker;
    NSObject <DatePickerControllerDelegate> *delegate;
}

@property (nonatomic, retain) UIDatePicker *datePicker;
@property (nonatomic, assign) NSObject <DatePickerControllerDelegate> *delegate;
@end

和 DatePickerController.m:

#import "DatePickerController.h"

@implementation DatePickerController

@synthesize datePicker;
@synthesize delegate;

- (void) loadView {
    self.view = [[[UIView alloc] init] autorelease];
    self.datePicker = [[[UIDatePicker alloc] init] autorelease];
    [self.view addSubview:self.datePicker];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"Done" forState:UIControlStateNormal];
    button.center = CGPointMake(160,230);
    [button addTarget:self action:@selector(done) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
}

- (void) done {
    [delegate datePickerController:controller didPickDate:datePicker.date];
}

- (void) dealloc {
    [datePicker release];
    [super dealloc];
}

@end

在主视图上,我有一个调用此类的按钮,如下所示:

#import "ModalDatePickerViewController.h"

@implementation ModalDatePickerViewController

- (void) pickDate {
    DatePickerController *screen = [[[DatePickerController alloc] init] autorelease];
    screen.delegate = self;
    [self presentModalViewController:screen animated:YES];
}

- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date {
    //[self doSomethingWithDate:date];
    [controller dismissModalViewControllerAnimated:YES];
}
- (IBAction)HitMe:(id)sender {
    [self pickDate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
    [super dealloc];
}
@end

and:

#import <UIKit/UIKit.h>
#import "DatePickerController.h"

@interface ModalDatePickerViewController : UIViewController <DatePickerControllerDelegate> {

}

- (IBAction)HitMe:(id)sender;

@end

在这一行:

[delegate datePickerController:controller didPickDate:datePicker.date];

尝试更换controller with self.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何实现模态日期选择器? 的相关文章

随机推荐

  • 如何一次刷新Postgresql 9.3中的所有物化视图?

    我正在将一堆数据加载到 PostgresQL 9 3 数据库中 然后我想刷新依赖于更新表的所有物化视图 有没有一种方法可以自动执行此操作 而不是逐一浏览每个视图并刷新它们 我知道 Oracle 可以很容易地做到这一点 但在梳理 Postgr
  • 使用单选按钮组打破键盘焦点

    这看起来很简单 但这对我来说有点令人头疼 给出以下 有效的 xhtml 过渡 代码
  • Angular CLI 将 font-awesome 字体文件输出到 dist root

    你一定要吗eject你的 angular cli 并覆盖 webpack 配置以放置font awesome字体放入 asset fonts 文件夹中 我有这个 angular cli 并且我以这种方式将 font awesome 包含在我
  • Python 特征向量

    eigenvalues eigenvectors linalg eig K 我怎样才能只打印特征向量len K 所以如果有K 2x2矩阵 我得到4个特征向量 如果有的话我怎样才能只打印其中2个len K 2 非常感谢 您将得到两个长度为 2
  • LLVM环境变量在哪里或者LLVM环境是如何配置的?

    作为上下文 我有一台 Mac 我在工作中使用它使用各种自定义构建脚本进行开发 我现在尝试使用同一台机器来玩一些个人 Xcode 项目 第一步 我在 Xcode 11 3 1 中创建一个新的 Single View App 项目 并尝试在我连
  • C# 中的内存使用情况

    我有一个在 C 中使用线程的程序 有没有办法以编程方式了解应用程序的内存使用情况 我想将线程的产生限制为 10 MB 内存 我该怎么做 如果您想要整个正在运行的进程的内存而不是每个线程的内存 那么 get the current proce
  • 用于出版质量图的 Python Pylab pcolor 选项

    我正在尝试使用 DFT 离散傅立叶变换 图pcolor在Python中 我之前一直使用 Mathematica 8 0 来执行此操作 但我发现 Mathematica 8 0 中的颜色条与我尝试表示的数据具有不良的一对一相关性 例如 这是我
  • Java:使用多个 DatagramSocket 接收 UDP 数据报包

    我正在尝试实现一种将 UDP 数据包发送到多个接收者的方法 我认为这应该是可行的设置setReuseAddress true 在接收 DatagramSocket 实例上 我的问题是 在某些情况下 我需要限制与本地计算机的通信 因此限制本地
  • 如何将RGB值转换为色温?

    如何获取以 RGB 值 例如 0 255 之间的三个坐标 表示的颜色并从中生成一个色温 https en wikipedia org wiki Color temperature在开尔文 或泥潭 I see 这个问题 https stack
  • for 循环:C++ 和 python

    我了解如何使用 for 循环 for x 0 x lt 5 x some code 在 C 中 但是 for 循环怎么样 for x in y 至少存在于 python 中 最接近的等效项是基于范围的 for 循环 例如 auto y 0
  • DreamHost 上的 Rails 3?

    我想在 dreamhost 上部署一个小型 Rails 3 应用程序 仅用于测试目的 没什么大不了的 我想知道是否有人已经这样做了 请选择以下选项之一 我做到了 非常简单 方法如下 虽然我没有尝试过 但应该很容易 方法如下 这很复杂 但这应
  • 使用具有根 UITabBarController 的故事板将 ManagedObjectContext 传递给视图控制器

    使用故事板 您无法轻松访问 appDelegate 中的第一个视图控制器 尽管一旦您这样做了prepareForSegue可以轻松地将 ManagedObjectContext 传递到导航堆栈中 我决定为每个需要核心数据访问的视图控制器 或
  • 更新到 OSX 10.9.5 后如何更改 Java 应用程序的签名方式

    刚刚从 OSX 10 9 4 更新到 10 9 5 看起来我必须更改更新后签署 Java 应用程序的方式 因为我现在在签名后得到此输出 export CODESIGN ALLOCATE Applications Xcode app Cont
  • Javascript:向函数原型添加方法

    有没有更短的写法 var controller function constructor controller prototype function1 function Prototype method1 controller protot
  • 我可以/应该编辑 R.java 文件吗?如果是这样,怎么办?

    因此 我正在 Eclipse for Android 4 0 中编写一个简单的计算器 并尝试简化我的代码并使其尽可能简单 我试图清理的地方是我的 findViewById 的 因为我有按钮 0 9 来实例化 所以我有一个十行长的代码块 如下
  • 主动类与被动类

    在 UML 规范上层结构 2 5 仍为 Beta 第 203 页 中 可以通过将 isActive 设置为 true 来指定类为活动类 即 它的每个实例都是一个活动对象 当 isActive 为 false 时 类是被动的 即 它的每个实例
  • 如何嵌入PDF?

    我正在尝试将 PDF 嵌入 HTML 文档 但这似乎仅适用于 Chrome 其他浏览器似乎要么需要插件 要么要求用户单击不是我想要的链接 这是我尝试过的 nbsp alt a href pdfFiles interfaces pdf 以下是
  • 在 Python 中求解 x 的高度非线性方程

    我正在尝试求解以下 dB 方程 为简单起见 我在问题标题中将 dB 表示为 x 方程中的所有其他项都是已知的 我尝试使用 SymPy 象征性地求解 dB 但我不断收到超时错误 我也尝试过使用fminbound from scipy opti
  • 使用 Matlab 进行多线程处理

    我正在开展一个 Matlab 项目 我们必须优化性能 并且我正在考虑并行化从 m 文件进行的几个函数调用 这个想法很简单 从 Matlab 文件 m 调用编译为 MEX 的 C 文件 然后从该 C 文件创建几个线程并从每个线程回调 matl
  • 如何实现模态日期选择器?

    我正在使用埃德 马蒂回答这个问题的代码here https stackoverflow com questions 1624866 iphone sdk how to implement a modal date picker但我在一些方面