使用“performSegueWithIdentifier”发送值时,目标值为null

时间:2014-02-21 23:09:47

标签: ios iphone

我在视图控制器中有三个导航按钮,其中两个在本地处理,另一个用于启动编辑视图控制器。我已经从第一个UITableView的主体创建了一个segue到目标视图控制器的导航控制器(编辑视图控制器)。 该按钮工作正常(即启动编辑视图)但它没有收到我发送给它的字符串。请有人帮忙。我已经尝试了其他相关问题的所有建议,但似乎无法解决这个问题。以下是相关代码。

来源controller.h:

#import <UIKit/UIKit.h>
#import "EQHorseAddViewController.h"
#import "EQDatabase.h"


@interface EQHorseDetailViewController : UITableViewController

@property (strong, nonatomic) EQHorseAddViewController  *addViewController;

来源controller.m:

#import "EQHorseDetailViewController.h"

@interface EQHorseDetailViewController ()

@end

@implementation EQHorseDetailViewController

@synthesize addViewController;

-(void)viewDidLoad
{
[super viewDidLoad];

UIBarButtonItem *deleteItem= [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
    target:self
    action:@selector(deleteHorse:)];
UIBarButtonItem *editItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self
action:@selector(editHorse:)];

NSArray *actionButtonItems = [[NSArray alloc] initWithObjects:deleteItem, editItem,  
nil];
self.navigationItem.rightBarButtonItems = actionButtonItems;
}

-(void)editHorse:(id)sender
{
NSLog(@"EditingHorse");
self.addViewController= [[EQHorseAddViewController alloc] init];
addViewController.resultSegue = @"Edit Horse Details";
[self performSegueWithIdentifier:@"EditHorseDetails" sender: self];
}

目的地视图Controller.h:

#import <UIKit/UIKit.h>
#import "EQHorseDetailsInfo.h"
#import "EQDatabase.h"

@class EQHorseAddViewController;

@protocol EQHorseAddViewControllerDelegate <NSObject>
- (void) eqHorseAddViewControllerDidCancel:(EQHorseAddViewController *)controller;
- (void)eqHorseAddViewControllerDidSave: (EQHorseAddViewController *)controller;
@end

@interface EQHorseAddViewController : UITableViewController   
<EQHorseAddViewControllerDelegate>

@property (strong, nonatomic) EQHorseAddViewController *addViewController;

@property (nonatomic, weak) id <EQHorseAddViewControllerDelegate> delegate;

@property (strong,nonatomic) NSString *resultSegue;

目标视图Controller.m

#import "EQHorseAddViewController.h"
#import "EQDatabase.h"
#import "EQHorseDetailsInfo.h"

@interface EQHorseAddViewController ()

@end

@implementation EQHorseAddViewController

@synthesize resultSegue;

- (void)viewDidLoad
{

 NSLog(@"resultSegue %@",resultSegue);

 }

提前感谢你。

3 个答案:

答案 0 :(得分:2)

看起来你在引用editHorse:中的代码,它分配EQHorseAddViewController并在其上设置属性。该代码的问题在于它分配的视图控制器与segue的vc目标无关。该方法只是创建一个vc然后立即丢弃它。

删除分配,改为实施prepareForSegue:。传递给该方法的segue对象将具有一个名为destinationViewController的属性。这就是即将推送到导航的视图控制器。那是您应该设置resultSegue属性的那个。

-(void)editHorse:(id)sender
{
    NSLog(@"EditingHorse");
    [self performSegueWithIdentifier:@"EditHorseDetails" sender: self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"EditHorseDetails"]) {
        EQHorseAddViewController *vc = segue.destinationViewController;
        vc.resultSegue = @"Edit Horse Details";
    }
}

答案 1 :(得分:0)

您需要按如下方式定义新控制器:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
EQHorseAddViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"EditHorseDetails"];
ivc.resultSegue = @"Edit Horse Details";

也许你也需要这种方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"EditHorseDetails"]) {

    // Get destination view
    EQHorseAddViewController *vc = [segue destinationViewController];
    vc.resultSegue = @"Edit Horse Details";   
   }
}

答案 2 :(得分:0)

-(void)editHorse:(id)sender
{
NSLog(@"EditingHorse");
resultSegue = @"Edit Horse Details";
[self performSegueWithIdentifier:@"EditHorseDetails" sender: self];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UINavigationController *navigationController = segue.destinationViewController;
    EQHorseAddViewController *addViewController = [navigationController viewControllers]
[0];

    // Get destination view

    NSLog(@"ResultSegue in Prepare - %@",resultSegue);

    addViewController.resultSegue = @"Edit Horse Details";
}