【问题标题】:How to get a reference to a method's caller object from within the method in Objective-C如何从Objective-C中的方法中获取对方法调用者对象的引用
【发布时间】:2010-05-14 12:53:20
【问题描述】:

我的应用程序视图上有一些文本字段和文本视图。当我点击其中任何一个时,键盘就会出现。我有一个方法,当这些对象中的任何一个被点击时都会被调用。但是,我希望该方法仅在点击某个文本字段或某个文本视图时才执行其代码。因此,我希望在方法主体中有这样的内容:

{
   if(currentField != mySpecialField)
      {return;}

     //Rest of the method code...
}

现在,我的问题是,如何获得对当前点击的字段的引用,以便我可以执行 if-check。

更新:

在我的情况下,我正在使用从 Apple 网站获得的代码,该代码利用了这些方法:

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    if (keyboardShown)
        return;

    NSDictionary* info = [aNotification userInfo];

    // Get the size of the keyboard.
    NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;

    // Resize the scroll view (which is the root view of the window)
    CGRect viewFrame = [scroller frame];
    viewFrame.size.height -= keyboardSize.height;
    scroller.frame = viewFrame;

    // Scroll the active text field into view.
    CGRect textFieldRect = [activeField frame];
    [scroller scrollRectToVisible:textFieldRect animated:YES];

    keyboardShown = YES;
}

// Called when the UIKeyboardDidHideNotification is sent
- (void)keyboardWasHidden:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];

    // Get the size of the keyboard.
    NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;

    // Reset the height of the scroll view to its original value
    CGRect viewFrame = [scroller frame];
    viewFrame.size.height += keyboardSize.height;
    scroller.frame = viewFrame;

    keyboardShown = NO;
}

我可以修改这些方法以获取对调用者 UITextField 和/或调用者 UITextView 的引用吗?

更新:

这是用于注册的方法:

// Call this method somewhere in your view controller setup code.


- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasHidden:)
                                                 name:UIKeyboardDidHideNotification object:nil];
}

有什么方法可以实现我想要实现的目标吗?也许如果我修改上面的代码以传递 'object:self' 而不是 'object:nil'?

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    作为UIControl 子类的控件(例如UIButtonUITextField)会自动向其目标发送对自身的引用。

    创建文本字段时,只需指定一个带有一个参数的选择器。在本例中,“myTextField”是一个实例变量:

    myTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
    [myTextField addTarget:self action:@selector(textFieldTapped:) forControlEvents:UIControlEventTouchUpInside];
    

    然后是我们指定的方法textFieldTapped:来处理事件:

    - (void)textFieldTapped:(UITextField *)sender {
        // Ignore any events that doesn't come from myTextField
        if (sender != myTextField) return;
    
        // TODO: Do stuff
    }
    

    UITextField 的情况下,您还可以查看UITextFieldDelegate 协议(文本字段的“代表”值)。它发出各种事件,可以用来注册同一种东西,例如:

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多