ios - 将数据传回 View Controller 的问题

标签 ios objective-c iphone xcode uitableview

尝试将数据从 tableviewcontroller 传回 viewcontroller 时的一个小问题。我在 View Controller 中有 3 个按钮和 3 个文本字段,当单击每个按钮时,将导航到具有静态数据(如头痛、发烧、咳嗽、感冒)的相同 TableView 。当我在 tableview 中选择一行时,问题就来了,数据被分配给 viewcontroller 中的所有 textfiled。但是我想当我选择 Fever 时单击 button1 时,它应该仅分配给 textfiled1,而 button2 是当我选择 Cough 时点击它应该只分配给第二个文本文件。下面是我的代码。

In **AppDelegate.h**

@property (strong, nonatomic) NSString *selectedText;


In **ViewController.m**

-(void)viewWillAppear:(BOOL)animated
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    txtField1.text = appDelegate.selectedText;
    txtField2.text = appDelegate.selectedText;
    txtField3.text = appDelegate.selectedText;

}


**FamilyDetailsViewController.m**

pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        NSString *selected= [arr objectAtIndex:indexPath.row];

        AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

        appDelegate.selectedText = selected;

       [self.navigationController popViewControllerAnimated:YES];
}

最佳答案

在第一个 View Controller 中有一个 int 变量,它应该可以在整个 View Controller 中访问。 当您按下按钮 1 时,将 int 变量的值设置为 1。对于其他按钮,将其更改为 2 和 3。 现在,根据该 int 变量分配 make 条件。

if(variable == 1){
txtField1.text = appDelegate.selectedText;
}
else if(variable == 2){
txtField2.text = appDelegate.selectedText;
}
else if(variable == 3){
txtField3.text = appDelegate.selectedText;
}

关于ios - 将数据传回 View Controller 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41564189/

相关文章:

objective-c - UICollectionView 特殊横流-多段

c++ - Xcode 中的奇怪日志

ios - 对多个 Xcode 目标使用相同的版本号 Info.plist

ios - 如何快速从颜色矩阵创建图像?

ios - RxSwift - UIImageView - 观察属性 isHighlighted

iOS : How to save selected row of tableview even I refresh tableview?

ios - 如何在 UIView 中显示所选 UICollectioNViewCell 的图像? ( swift 3)

iphone - 如何将字符串从一个 uiviewcontroller 传递到另一个

ios - 如何在 Swift 中使用拉取刷新?

ios - obj-c AFNetworking 2.0 POST 请求不起作用