从另一个类的按钮操作填充textfield中的数据

时间:2016-04-20 06:18:05

标签: ios objective-c

我的问题是我的viewcontroller上有一个文本字段,应由用户填写并提供填充文本文件的选项,文本字段前面有一个箭头按钮。单击它打开一个带有选项列表的新viewcontroller。因此,当用户单击该视图控制器中的任何选项时,数据将自动填充该文本字段,并且用户将返回上一个viewcontroller。我不能在其中使用preprefor segue,所以请提供答案。如果我遗失了某些东西让我知道我会评论或编辑。

4 个答案:

答案 0 :(得分:1)

您可以使用NSNotificationCenter填写UITextField。将观察者添加到您要填充viewController的第一个class textField。在viewDidLoad -

中添加以下代码行
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fillMyTextField:) name:@"fillMyTextField" object:nil];

而不是在同一个类中添加selector方法 -

- (void)fillMyTextField:(NSNotification *)notification
{

    self.myTxtField.text = (NSString *)notification.userInfo;
}

现在,您可以在其他viewController class中选择textField的数据。在选择数据的方法中写下面的代码,如 -

- (IBAction)selectDataAndBackToPreviosVC:(UIButton*)selectedOptionBtn {

     id object=[NSString stringWithFormat:@"%@",selectedOptionBtn.titleLabel.text];

     [[NSNotificationCenter defaultCenter] postNotificationName:@"fillTextField" object:nil userInfo:object];

     [self.navigationController popViewControllerAnimated:YES];

}

在此我使用navigationViewController使用PushPop viewControllers,我在选项UIButton中有一些viewController。我在myTextField上传递了IBAction选项的按钮标题。

答案 1 :(得分:0)

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fillTextFieldAction:) name:@"fillTextField" object:nil];


- (IBAction)fillTextFieldAction:(NSNotification *)notification
{
    textField.text = (NSString *)notification.object
}

    [[NSNotificationCenter defaultCenter] postNotificationName:@"fillTextField" object:nil userInfo:textFieldInfo];

答案 2 :(得分:0)

在textfield Define Below方法(添加通知观察者)

的类中

在viewDidLoad

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fillTextField:)
                                                 name:@"fillTextField" 
                                               object:nil];

- (IBAction)fillTextField:(NSNotification *)sender 
{
    textField.text = (NSString *)notification.object
}

在另一个类中,您在日期选择操作

中选择以下方法设置日期
[[NSNotificationCenter defaultCenter] postNotificationName:@"fillTextField" object:nil userInfo:textFieldInfo];

答案 3 :(得分:0)

您可以使用NSNotification:

在View控制器ofTextField中添加此

- (void) viewDidLoad
{

//Your rest of code then below statement

      [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(receiveTextNotification:) 
            name:@"TestNotification"
            object:nil];
}

- (void)receiveTextNotification:(NSNotification *)notification {
    NSLog(@"%@ updated", [notification userInfo]);
}

- (void) dealloc
{
     // If you don't remove yourself as an observer, the Notification Center
    // will continue to try and send notification objects to the deallocated
    // object.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

在按钮的View控制器中,在按钮上单击添加:

- (IBAction)fillTextFieldAction: (id) sender    {
         // All instances observing the `TestNotification` will be notified
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self userInfo:text];   //Object here can be any changed value .
    }
相关问题