- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码用当前时间更新文本字段的值。我的问题是:为什么在代码的底部发送 nil 作为发件人? [self showCurrentTime:nil];
CurrentTimeViewController.m
- (IBAction)showCurrentTime:(id)sender
{
NSDate *now = [NSDate date];
static NSDateFormatter *formatter = nil;
if(!formatter) {
formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
}
[timeLabel setText:[formatter stringFromDate:now]];
}
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"CurrentTimeViewController will appear");
[super viewWillAppear:animated];
[self showCurrentTime:nil];
}
最佳答案
因为通常在调用 Action 处理程序时,它们会将发起调用的对象传递给方法,例如 UIButton
, 或 UISegmentedControl
.但是,当您想从代码中调用方法时,而不是作为操作的结果,您不能将人类作为 sender
传递。 , 所以你通过 nil
.
此外,- (IBAction)
表示此方法可以通过 Interface Builder
连接到事件通过拖动 Touch Up Inside
(或触碰外部/等)事件从按钮(或任何其他具有某种事件的控件)到 File's Owner
并选择 thumbTapped:
.
例如:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = 1001;
[button addTarget:self action:@selector(thumbTapped:) forControlEvents:UIControlEventTouchUpInside];
thumbTapped:
,通过
button
对象为
sender
- (IBAction)thumbTapped:(id)sender {
if ([sender isKindOfClass:[UIButton class]] && ((UIButton *)sender).tag == 1001) {
iPhoneImagePreviewViewController *previewVC = [[iPhoneImagePreviewViewController alloc] initWithNibName:@"iPhoneImagePreviewViewController" bundle:nil];
[self.navigationController pushViewController:previewVC animated:YES];
[previewVC release];
} else {
[[[[UIAlertView alloc] initWithTitle:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]
message:@"This method was called from somewhere in user code, not as the result of an action!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
}
}
关于iphone - (id)sender = nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7623120/
我对 Objective-C 很陌生,如果这对你们中的许多人来说非常明显,我很抱歉,但我正在尝试弄清楚以下代码是如何实际工作的: - (IBAction)chooseColour:(UIButton
我有一个 UITableView,它从代表标题、摘要和目标 URL 的字符串数组中获取数据。当用户选择一个单元格时,应用程序会转到包含 WebView 的 View Controller 。 问题是无
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: C# “as” cast vs classic cast 这两个表达式有什么区别? (ListView)发送
目前正在学习objetive-c。浏览了各种书籍以及 lynda 网站上的一些教程,现在找到了我认为适合我学习风格的斯坦福类(class)。我一直在一遍又一遍地看前 3 个讲座,基本上是在做,直到我
当我为我的业务对象编写公共(public)事件时,除了额外的特定参数外,我已经习惯了始终将实例作为“发送者作为对象”传递。我现在只是问自己为什么我不指定类? 所以对于有更多经验的你; 您是否曾经在事件
在 Objective-C 中,“(UIButton *)sender”是什么意思,为什么不是“UIButton *sender”?或者一些 NSObject 代替 UIButton。这更多是关于星号
我正在尝试学习用于 iPhone 开发的 Objective-C,并且在浏览有关操作的示例时,我经常看到如下内容: - (IBAction)sliderMoved:(id)sender { U
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: Objective C: what is a “(id) sender”? in 的 (id)sender 是
我已经查看了我能找到的关于这个问题的所有文档,但无法弄清楚这一点。 一定有人在使用 Sendgrid 作为 SMTP 用于话语博客,但我不知道如何做到。 这是我的 app.yml 设置: env:
当我将按钮链接到源代码(在本例中为 .h 文件)并选择 NSButton 作为类型时,Xcode 将写入: - (IBAction)pushClear:(id)sender; 据我了解应该是 - (I
我有一个代码可以使用 randomLabel 作为触发事件的 sender。在此代码中,我尝试调整 sender 的字体属性,但它似乎给出了编译错误。有什么办法可以解决这个问题吗? priva
我有一个包含 16 个 TCheckBox 和 32 个 TEditBox 的表单。每 2 个 TEditBox 的启用取决于复选框的状态。所以我使用了这段太长的代码: //T1 procedure
我正在编写一个 Chrome 扩展,其中内容脚本向后台脚本发送消息。 chrome.runtime.sendMessage({greeting: "hello"}, function(response
我对 Winform 控件中 sender 参数的用途感到困惑,例如: Private Sub Form1_Load(sender As System.Object, e As System.Even
我有以下代码用当前时间更新文本字段的值。我的问题是:为什么在代码的底部发送 nil 作为发件人? [self showCurrentTime:nil]; CurrentTimeViewControll
我编写了一个 Java 线程(可运行)来基本上存储 DatagramPackets 列表,如果列表的大小 > 0,它将发送列表中的第一项。 看来,除非我在 run 方法中放置大量 sleep ,否则线
我正在使用位于 github here 上的 pjjanak 的 chromecast-hello-world 应用程序测试我的 chromecast . 他向 message 事件添加了一个监听器,
当我在 Laravel 5.1 中重置密码时,我收到了电子邮件,但标题中显示“无发件人”。 有没有办法从某处指定密码重置电子邮件的发件人?除了发件人,我假设 Laravel 应该自动使用配置文件中指定
我总是收到此错误,而且我不知道还要输入什么。 你们知道这是怎么回事吗? @IBAction func loginAction(sender: AnyObject) { let email =
我想制作一个按钮,使其在点击后保持按下状态。所以我制作了一个插槽 make_pushed ,我试图将其用于该目的。单击的按钮由 QObject::sender() 方法标识。但是出了点问题,因为它不起
我是一名优秀的程序员,十分优秀!