【问题标题】:Passing Parameter From a View Back To another View's UITableView's Cell将参数从视图传递回另一个视图的 UITableView 的单元格
【发布时间】:2011-05-11 10:23:48
【问题描述】:

我有两个观点。 第一个:FirstViewController 第二:SecondViewController

FirstViewController 是我的 UINavigationController 的根控制器,在 FirstViewController 内部我有 UITableView。当在 UITableView 中单击一个单元格时,视图将导航到 SecondViewController。在 SecondViewController 里面我有 UILabel。我想将此 UILabel 的值分配给在 FirstViewController 在导航栏中单击后退按钮时单击的单元格。我应该怎么做才能实现这个?

我可以通过创建将值从 FirstViewController 传递给 SecondViewController:

SecondViewController *sv; sv.somestring = someanotherstring;

但无法在 SecondViewController 中实现此功能以将值传递给 FirstViewController 中的 NSString。

你能帮帮我吗?

谢谢。 ae

【问题讨论】:

    标签: iphone uitableview uinavigationcontroller


    【解决方案1】:

    在 iPhone SDK 中处理此问题的典型方法是定义委托协议。例如:

    @protocol SecondViewControllerDelegate
    - (void) viewControllerWillDisappearWithLabelText: (NSString*)text;
    @end
    

    然后你会添加一个delegate 属性到你的SecondViewController,比如:

    //in the .h file
    @interface SecondViewController : UIViewController {
        //declare instance variables
    }
    @property(nonatomic, assign) id<SecondViewControllerDelegate> delegate;
    @end
    
    //in the .m file
    @implementation SecondViewController
    
    @synthesize delegate;
    
    //[code...]
    @end
    

    然后您将更新 FirstViewController 以实现委托协议:

    //in the .h file
    @interface FirstViewController : UIViewController<SecondViewControllerDelegate> {
        //[instance variables]
    }
    //[methods and properties]
    @end
    
    //in the .m file
    @implementation FirstViewController
    //[code...]
    
    - (void) viewControllerWillDisappearWithLabelText: (NSString*)text {
        //do whatever you need to do with the text
    }
    
    //[code...]
    @end
    

    ...并在FirstViewController 创建SecondViewController 时设置委托字段:

    SecondViewController* sv = [[SecondViewController alloc] init]; 
    sv.somestring = someanotherstring;
    sv.delegate = self;
    

    最后,在SecondViewController 中实现viewWillDisappear 大致如下:

    - (void) viewWillDisappear: (bool)animated {
        [super viewWillDisappear:animated];
        if (self.delegate) {
            [self.delegate viewControllerWillDisappearWithLabelText: myLabel.text];
        }
    }
    

    【讨论】:

      【解决方案2】:

      是的,有很多简单的方法来处理这个.....

      你可以取一个全局变量

      在您的 Delegate.h 文件中声明您的变量:

      @interface Smoke_ApplicationAppDelegate : NSObject {
      
        UIWindow *window;
        UINavigationController *navigationController;
        NSString *messageString;  //This would be your String Variable
      }
       @property(nonatomic,retain)NSString *messageString;
      

      其次在 Delegate.m 文件中

      @implementation Smoke_ApplicationAppDelegate
      
      @synthesize window; 
      @synthesize navigationController; 
      @synthesize messageString; // Synthesize it over here..
      

      这已经完成了。现在你可以在所有/任何你想要的类中使用这个字符串变量了。

      使用这个全局变量。

      只需导入你的委托文件使其成为obj....

      import "DelegateFile.h"
      
      @implementation About
      
      DelegateFile *appDel;
      

      现在在你的班级.m

      -(void)viewDidLoad { [super viewDidLoad];
      
      appDel=[[UIApplication sharedApplication]delegate];
      
      }
      

      现在你可以通过这个对象在你班级的任何地方访问它:

      appDel.messageString
      

      请仔细按照我的步骤进行,我相信这肯定会对您有所帮助.....

      祝你生活愉快

      【讨论】:

      • 感谢您接受答案。您仍然可以通过制作其他类的对象并通过合成来使用它来用其他方式做同样的事情。
      【解决方案3】:

      在appdeleage中声明一个字符串(stringVal)并将其属性设置为非原子并保留,也合成它。在第二个视图控制器中,您可以将标签值设置为appdelegate字符串([appdelegate setStringVal:label.text]; ) .您可以在第一个视图控制器中获取此值并在 table(NSString *localString=appdelegate.stringVal];) 中使用它。

      一切顺利。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-04
        • 1970-01-01
        • 1970-01-01
        • 2020-03-03
        • 1970-01-01
        • 2017-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多