将字符串传递给ViewController不会显示

时间:2014-11-12 19:37:05

标签: ios objective-c

所以我试图让一个非常基本的项目完成;根据按钮,第二个视图上会显示不同的文本。我已经在这几个小时了,它已经到了我无法想到的任何其他事情。我已经在这里提出了一些建议,但我遇到第二个ViewController实际获取信息的问题。

我看到之前已经问过这个问题,我不能把它放在一起,我自己......

//ViewController.h



#import <UIKit/UIKit.h>
#import "SuggestionViewController.h"
@class SuggestionViewController;
@interface ViewController : UIViewController{
SuggestionViewController * suggestionViewController;
}

- (IBAction)burger:(id)sender;
- (IBAction)pasta:(id)sender;
- (IBAction)chicken:(id)sender;


@end

-

//ViewController.m


#import "ViewController.h"
#import "SuggestionViewController.h"

@interface ViewController ()

@end

@implementation ViewController

NSString *valueTxt = @"example";

- (void)viewDidLoad {
    [super viewDidLoad];
    suggestionViewController=[[SuggestionViewController alloc]initWithNibName:@"SuggestionViewController" bundle:nil];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)burger:(id)sender {
    //value1=[NSString stringWithFormat:@"Burger"];
    suggestionViewController.suggestionText = valueTxt;
    [self performSegueWithIdentifier:@"bSe" sender:sender];


}

- (IBAction)pasta:(id)sender {
    [self performSegueWithIdentifier:@"bSe" sender:sender];
   suggestionViewController.suggestionText = [NSString stringWithFormat:@"pasta"];
}

- (IBAction)chicken:(id)sender {
    [self performSegueWithIdentifier:@"bSe" sender:sender];
    suggestionViewController.suggestionText = [NSString stringWithFormat:@"chicken"];
}
    @end

-

//SuggestionViewController.h

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    #import "ViewController.h"

@interface SuggestionViewController : UIViewController


@property (strong, nonatomic) NSString* suggestionText;

@end

-

//SuggestionViewController.m

#import "SuggestionViewController.h"
#import "ViewController.h"

@interface SuggestionViewController ()
@property (weak, nonatomic) IBOutlet UILabel *suLabel;


@end

@implementation SuggestionViewController
@synthesize suggestionText;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    //SuggestionViewController *vc = [[SuggestionViewController alloc] init];
    //vc.suLabel.text = suggestionText;
    _suLabel.text = suggestionText;
} 

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

@end

3 个答案:

答案 0 :(得分:0)

问题在于,当您说suggestionViewController时,您在界面中看到的[self performSegueWithIdentifier:@"bSe" sender:sender];实例与您一直保留并配置的suggestionViewController实例不同。< / p>

您要做的是实施prepareForSegue并在那里执行所有配置。这就是它的用途。

或者不使用故事板。

答案 1 :(得分:0)

从代码中删除suggestionViewController。并实现PrepareForSegue方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    SuggestionViewController *suggestionViewController = [segue destinationViewController];
    suggestionViewController.suggestionText = valueTxt;
}

您正在将值设置为SuggestionViewController的另一个实例,而不是使用segue创建的实例。

segue方法的

prepareForSegue是传递值以查看segues创建的控制器的地方。

答案 2 :(得分:0)

当您使用故事板时,performSegueWithIdentifier将为您分配目标视图控制器的新实例 - 因此您正在创建的实例不会被使用。

您可以更改属性以存储要发送的字符串,然后在prepareForSegue -

中的目标位置设置此属性

<强> ViewController.h

#import <UIKit/UIKit.h>

- (IBAction)burger:(id)sender;
- (IBAction)pasta:(id)sender;
- (IBAction)chicken:(id)sender;


@end

ViewController.m

#import "ViewController.m"
#import "SuggestionViewController.h"

@interface ViewController()

@property (copy,nonatomic) NSString *valueText;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)burger:(id)sender {
    self.valueText=@"Burger";
    [self performSegueWithIdentifier:@"bSe" sender:sender];


}

- (IBAction)pasta:(id)sender {
    self.valueText=@"pasta"
    [self performSegueWithIdentifier:@"bSe" sender:sender];
}

- (IBAction)chicken:(id)sender {
    self.valueText=@"chicken"
    [self performSegueWithIdentifier:@"bSe" sender:sender];
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"bSe"]) {
        SuggestionViewController *svc=(SuggestionViewController *)segue.destinationViewController;
        svc.suggestionText=self.valueText;
    }

@end