English 中文(简体)
(id)sender = nil?
原标题:(id)sender = nil?

我有以下法典,目前更新文本领域的价值。 我的问题是:为什么你把 n子作为手寄到法典的底部?

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];
}
最佳回答

由于通常在要求采取行动的操作员时,他们通过向该方法发出呼吁的标语,如<条码>、IDButton或<条码>。 但是,如果你想从你的法典而不是由于一项行动而使用这种方法,你不能把人当作<条码>,<> > ,这样你就通过<条码>。

另外,-(IBAction)表示,这一方法可以通过<代码>与某项活动联系起来。 Interface Buildinger, 摘录Touch Upside (或在外/上下)活动,从纽伦(或具有某种事件的任何其他控制)到File s Owner, 并选定thumbTapped:

例如:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = 1001;
[button addTarget:self action:@selector(thumbTapped:) forControlEvents:UIControlEventTouchUpInside];

亮点放出后(电线仍在 but子内),将打电话<条码>。 标 标

- (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];
    }
}
问题回答

国际行动方法以最常见的形式,仅举一句话。 当系统援引时,他们通过控制,触发行动,作为投标参数。 如果你将直接使用这种方法,你就需要提供一名发人。 由于这种方法没有被用户刚才互动的控制权所援引,因此使用无。

我实际上认为,直接要求采取行动,就是一个良好的模式。 采用一种与标签IBAction的方法意味着“这一方法是针对用户行动而采用的”,这是一个重要的背景,可以依靠这种方法开展工作。 如果法典将直接指该方法,该想法就会被违反。

我通常认为最好做这样的事情:

- (void)updateTime:(NSDate *)date {
    static NSDateFormatter *formatter = nil;
    if(!formatter) {
        formatter = [[NSDateFormatter alloc] init];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
    }

    [timeLabel setText:[formatter stringFromDate:date]];
}

- (IBAction)showCurrentTime:(id)sender {
    [self updateTime:[NSDate date]];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self updateTime:[NSDate date]];
}

这是因为你需要寄出intthing,因为你再次要求的方法的签名就是一个论点。

The (id)sender is used in button press actions. 当你用刀子把一个子连接到你的控制器的一种方法时,它将检查它是否能够提出论点。 如果是的话,“发送人”将指在刀子中创建的<条码>。

在方案县的建立以及你派任选人的其他许多情况也是如此。

The specifcation for the showCurrentTime function has 1 argument named sender. If you called the function without sending the ID of an object the call would be invalid.

Nil被客观地使用,而不是民族解放军,而是被派去履行你要求的职能。

As the function does not actually use the argument within the body it does not actually matter what object you send to the function.





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签