开发者

Xcode: Why doesn't this code call the action in another class?

开发者 https://www.devze.com 2023-04-11 20:23 出处:网络
I have two view controllers. In one of them I have a UITextField within a UITableViewCell. What I want is to call a action in another view controller when the UITextField is being edited. In the - (UI

I have two view controllers. In one of them I have a UITextField within a UITableViewCell. What I want is to call a action in another view controller when the UITextField is being edited. In the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

I use this code to call the action:

[TextFieldText addTarget:Viewcontroller1 action:@selector(ApplyAllObjectsSettings:) forControlEvents:UIControlEventEditingChanged];

The action is in Viewcontroller1 and it looks like this:

- (void)ApplyAllObjectsSettings {
    NSLog(@"Test");
    // Test
}

I have inserted the action in the .h file.

The weird thing is that I have used almost the same code earlier and it worked fine. I think the only difference is that the view controller the UITextfi开发者_C百科eld is in is shown from Viewcontroller1 by presentmodalviewcontroller. and where the code that worked was presented it was by addsubview. Don't know if this have anything to say.

Thanks in advance :)


Your selector is wrong. With the trailing colon in @selector(ApplyAllObjectsSettings:) the method you want is not going to be called because it takes no arguments. @selector(ApplyAllObjectsSettings:) is very different from @selector(ApplyAllObjectsSettings). Either change your ApplyAllObjectsSettings method to this: - (void)ApplyAllObjectsSettings:(id)sender, or your selector to this: @selector(ApplyAllObjectsSettings).

So, either do this:

[TextFieldText addTarget:Viewcontroller1 action:@selector(ApplyAllObjectsSettings:) forControlEvents:UIControlEventEditingChanged];
// ...
- (void)ApplyAllObjectsSettings:(id)sender {
    NSLog(@"Test");
    // Test
}

Or this:

[TextFieldText addTarget:Viewcontroller1 action:@selector(ApplyAllObjectsSettings) forControlEvents:UIControlEventEditingChanged];
// ...
- (void)ApplyAllObjectsSettings {
    NSLog(@"Test");
    // Test
}

From the capitalization of the syntax, it appears that your target is a class name. The addTarget: parameter needs to be an instance of an object, not the class name.

Viewcontroller1 *vc1 = [[Viewcontroller1 alloc] initWithBlahBlah...];
[TextFieldText addTarget:vc1 action:@selector(ApplyAllObjectsSettings:) forControlEvents:UIControlEventEditingChanged];
0

精彩评论

暂无评论...
验证码 换一张
取 消