【问题标题】:Passing value between two view - Xcode 4.2 with storyboard在两个视图之间传递值 - 带有情节提要的 Xcode 4.2
【发布时间】:2012-05-09 10:16:54
【问题描述】:

我是使用故事板进行 Xcode 编程的新手。 我正在尝试将一个值(标签的文本)从“SecondViewController”传递给“View Controller”。 这是我的代码:

ViewController.m

- (IBAction)showSecondView:(id)sender {
    NSLog(@"Trying..");
    // The identifier used here is set on the second view controller in the UIStoryboard.
    SecondViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondIdentifier"];
    NSLog(@"Test: %@",svc.labelTest.text);  //this print NIL!!
    svc.labelTest.text = @"GOOFY!!";
    [self.navigationController pushViewController:svc animated:YES];
}

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

- (IBAction)returnToFirstView:(id)sender;
- (IBAction)toTabBar:(id)sender;
@property (strong, nonatomic) IBOutlet UILabel *labelTest;

@end

SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController
@synthesize labelTest;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

有人可以帮帮我吗? 非常感谢!

斯特凡诺

【问题讨论】:

    标签: uitableview xcode4.2 storyboard


    【解决方案1】:

    如果你使用故事板,为什么不使用- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    替换你的- (IBAction)showSecondView:(id)sender { -方法

    与:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SecondIdentifier"]) {
    SecondViewController *svc = (SecondViewController *)segue.destinationViewController;
    svc.labelTest.text = @"Goofy";
    }
    

    【讨论】:

    • 没问题 :) 如果它比接受答案不是为我而是为其他人提供帮助。因此,如果他们有类似的问题,他们可以选择最佳解决方案。它帮助了你和 stackoverflow..
    猜你喜欢
    • 2012-02-03
    • 2012-06-02
    • 2012-03-11
    • 2013-10-27
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    相关资源
    最近更新 更多