2012-01-06 11 views
-2

私は2つのUITableViewController(UINavigationControllerに接続)を持っています。最初のコントローラでUITableViewCellをクリックすると、セグを作成して、2番目のコントローラが表示されるようにしました。私は、選択された上のテキストを含む変数を2番目のビューコントローラに入れたいと思っています。これはどうすればいいですか?私はそれを直接渡すことを試みたが、それは何らかの理由でうまくいかない。プロパティをUITableViewControllerに渡す最も簡単な方法

答えて

0

受信viewController(この場合はSecondTableViewControllerと呼びます)でプロパティ(selectedText - NSStringなど)を宣言します。ここでは、コードは次のようになります。SecondTableViewController.hで

:SecondTableViewController.mで

@interface SecondTableViewController : UITableViewController { 

} 

@property(nonatomic, strong) NSString *selectedText; 

あなたFirstTableViewController.mで
@implementation SecondTableViewController 
@synthesize messageDetail; 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Make sure we are dealing with the proper Segue 
    if ([segue.identifier isEqualToString:@"idOfMySegue"]) { 
     SecondTableViewController *svc = segue.destinationViewController; 
     svc.selectedText = myVarValue; // This can be got from either setting another var in the tableviewcontroller or by just passing the entire object at the selected index of the NSIndexPath.row (if you are populating the tableview with an array. Note you would change the object type of the passed along object from an NSString to whatever the other object is. 
    } 
} 

prepareForSeque方法がされあるビューから別のビューにデータまたはオブジェクトを渡します。

+0

唯一の問題は、prepareForSegue(prepareForSegueが入力されたクラス)内のクラスプロパティにアクセスできないことです。 – blake305

+0

FirstTableViewController.mファイルのSecondTableViewControllerへの参照を含める限り、アクセスできます。 –

+0

あなたが今持っているものを私に送ってください。私はあなたが問題を理解するのを手伝ってくれるでしょう。 –