0

初めての投稿です。評判が 10 必要なため、画像をアップロードできません。XD でも問題ありません。

ここに画像の説明を入力

こんにちはみんな、まあ、2番目または子ビューコントローラーからテーブルビューのセルからlabel.textを最初または親ビューコントローラーに渡す必要があります。セルをプッシュして、ラベルにロードする文字列に戻ります最初のView Controllerから、ストーリーボードのセグエを使用しました。プロトコルとデリゲートを使用しましたが、機能しませんでした。別の質問では、回答または例が必要なものとは反対であるか、xcodeバージョンとは非常に異なる方法です。または、これを行うための最良の方法は何ですか?、写真では、次のView Controllerに移動するためのボタンを使用して、最初のView Controllerで必要なラベルテキストを選択します.Xcode 4.6とストーリーボードとARCも使用しています. &コードのいくつかの行は非推奨です。みんなを助けてください!! & どうもありがとう!!!、

ボリビアからのご挨拶!!! n_n'

コードは次のようになります。

     @interface FirstViewController : UIViewController <SecondTableViewControllerDelegate>

    @property (copy, nonatomic) NSString *displayText;
    @property (weak, nonatomic) IBOutlet UILabel *displayLabel;
    @property (strong, nonatomic)IBOutlet UIButton *Next;

    @end

    @interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize Next;


    -(void)viewDidLoad
    {
        [super viewDidLoad];
        if (![[NSUserDefaults standardUserDefaults] objectForKey:@"FIRST_TIME"])
        {
            NSLog(@"ES PRIMERA VEZ");
            [Next sendActionsForControlEvents:UIControlEventTouchUpInside];
        }
        else
        {
            NSLog(@"NO ES PRIMERA VEZ");
        }


    }

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];

        if (self.displayText)
        {
            self.displayLabel.text = self.displayText;
        } else {
            self.displayLabel.text = kDefaultDisplayText;
        }
    }

    #pragma mark - Delegate methods

    - (void)updateText:(NSString *)text {
        self.displayText = text;
    }

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if (![segue.identifier isEqualToString:@"toSecondTableview"]) {
            return;
        }
        /*
        SecondTableViewController *destinationController = segue.destinationViewController;
        destinationController.delegate = self;
        destinationController.editText = self.displayText;
        */

        //-- if I pass data from the first view controller to the second right?

    }

    @end

in the second view

    @protocol SecondTableViewControllerDelegate <NSObject>

    @optional
    - (void)updateText:(NSString *)text;

    @end

    @interface SecondTableViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

    @property (weak, nonatomic) id<SecondTableViewControllerDelegate> delegate;
    @property (copy, nonatomic) NSString *editText;
    @property (weak, nonatomic) IBOutlet UITableView *myTable;


    @interface SecondTableViewController ()

    @end

    @implementation SecondTableViewController

    -(void)viewDidLoad
    {
        [super viewDidLoad];
        [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"FIRST_TIME"];

         //---how use the UItable Methods?, how use the protocol method?
    }


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.myArray.count;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"myArray";

        myArrayCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        NSDictionary *conjunto = [self.jogArray objectAtIndex:indexPath.row];

        cell.myLabel.text = [conjunto objectForKey:@"prueba"];


        return cell;
    }

または、プロトコルデリゲートは他のView Controllerにある必要がありますか?、次のView Controllerの任意のセルのテキストをラベルに表示したいのですが、どうすればよいですか??

4

1 に答える 1

0

デリゲートパターンを使用します。テキストがハンドレットである方法でプロトコルを定義します。

- (void) labelText:(NSString*)text;

とか、ぐらい。プロトコルを実装するpartentviewコントローラーを用意します。それがと呼ばれているとしましょうYourProtocol。子ビューコントローラーで、タイプのデリゲートプロパティを宣言します(id)<YourProtocol>。partentビューコントローラでに割り当てselfますchildControler.delegate。テキストを割り当てようとしているときに、デリゲートがプロトコルに同意するか、セルクターlabelText:に応答するかを再確認してから、それを呼び出します。

実際よりも複雑に聞こえ、一種の「クリーン」です。

于 2013-03-05T15:48:31.167 に答える