将NSString设置为segue的发送端

时间:2013-08-14 13:45:21

标签: iphone objective-c segue

我做了一个简单的项目来解释我的问题。基本上我所做的是用Master Detail Application的模板进行项目。然后在

DetailViewController.h

没有

DetailViewController.m

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"showDetail"])
    {
        [segue.destinationViewController setCellName2:@"New String"];
    }
}

MasterViewController.h

@property(nonatomic,strong) NSString *cellName2;

MasterViewController.m

@synthesize cellName2;

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

}

我的问题出在我设置cellName2的detailViewController中,我无法设置它,因为detailViewControllerreceivingViewController的{​​{1}}。他们的方法是设置segView的sendingViewController结束吗?


修改

在Firo回答之后,我的代码现在看起来像这样

MasterViewContoller.h

segue

的.m

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface MasterViewController : UITableViewController <DetailViewDelegate>

@property(nonatomic,strong) NSString *cellName2;


@end

DetailViewContoller.h

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation MasterViewController

@synthesize cellName2;

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

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

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;

    self.cellName2 = cellName;

    NSLog(@"%@", self.cellName2);

}

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

- (void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

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

        /* assigning self as delegate, telling the detail view that I implement
         * setCellName2:, so it (the detailVC) can call it whenever it wants to.
         */
        [segue.destinationViewController setDelegate:self];
    }
}

// my implementation of the DetailViewDelegate protocol that I abide to

/* note: #pragma mark is not required, just for comment, documentation
 * and find-ability purposes
 */
//#pragma mark - DetailViewDelegate

// note: this is just a property setter so this is not actually needed
//- (void)setCellName2:(NSString *)cellName {
//    self.cellName2 = cellName;

//    NSLog(@"%@", self.cellName2);
//}

@end

的.m

#import <UIKit/UIKit.h>

/* defining a protocol, whoever is a DetailViewDelegate must implement my
 * defined methods
 */
@protocol DetailViewDelegate <NSObject>
- (void)setCellName2:(NSString *)cellName;
@end

@interface DetailViewController : UIViewController

/* storing a delegate property. Whoever sets themselves to my delegate
 * must implement my DetailViewDelegate's methods (setCellName2: in this case)
 */
@property (weak, nonatomic) id<DetailViewDelegate> delegate;

@property (strong, nonatomic) id detailItem;

@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;


@end

现在的问题是,我收到错误“使用未声明的标识符'cellName';你的意思是'cellName2'吗?”

2 个答案:

答案 0 :(得分:3)

您正在寻找代表和协议。由于您没有从detailVC转到masterVC,因此无法将此setter放入prepareForSegue。您需要存储对主服务器的引用并对其进行回调。以下是您使用基本示例的方法:

DetailViewController.h

/* defining a protocol, whoever is a DetailViewDelegate must implement my
 * defined methods
 */
@protocol DetailViewDelegate <NSObject>
- (void)setCellName2:(NSString *)cellName;
@end

@interface DetailViewController : UIViewController

/* storing a delegate property. Whoever sets themselves to my delegate
 * must implement my DetailViewDelegate's methods (setCellName2: in this case)
 */
@property (weak, nonatomic) id<DetailViewDelegate> delegate;
@end

DetailViewController.m

@implementation DetailViewController

// some action or method
- (IBAction)buttonPress:(id)sender {
    // look below at Master's prepareForSegue

    /* calling the method that my delegate implements, my delegate can be any
     * object that implements my protocol (DetailViewDelegate)
     */
    [self.delegate setCellName2:@""];
}

@end

MasterViewController.h

#import "DetailViewController.h"

// saying I implement the DetailViewDelegate protocol (and all necessary methods)
@interface MastViewController : UITableViewController <DetailViewDelegate>
@end

MasterViewController.m

@interface MasterViewController ()
@property(strong, nonatomic) NSString *cellName2;
@end

@implementation MasterViewController

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

        /* assigning self as delegate, telling the detail view that I implement
         * setCellName2:, so it (the detailVC) can call it whenever it wants to.
         */
        [segue.destinationViewController setDelegate:self];
    }
}

// my implementation of the DetailViewDelegate protocol that I abide to

/* note: #pragma mark is not required, just for comment, documentation
 * and find-ability purposes
 */
#pragma mark - DetailViewDelegate

// note: this is just a property setter so this is not actually needed
- (void)setCellName2:(NSString *)cellName {
    _cellName = cellName

    NSLog("%@", self.cellName);
}

@end

我会向您提供有关委托和协议的更多信息,但在处理iOS开发时这是一种非常常见的模式。您也应该对代理和协议非常熟悉,您会发现它在许多情况下都很有用,它将帮助您更好地理解iOS开发并使您成为更有能力的程序员。如果某些东西不起作用(或有意义),请告诉我。我只是在SO中打字,所以可能会有一些轻微的错误。

编辑

注意:如果这与原始问题相差甚远,您可能需要创建一个新问题。

您的主要问题是self.cellName2 = cellName;中有MasterViewController。根据您的原始帖子,您希望DetailVC设置此权限,对吧?因此,需要进入DetailViewController的{​​{1}}:

viewDidLoad

然后删除:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.delegate setCellName2:@"My custom text!"];
}

来自self.cellName2 = cellName; NSLog(@"%@", self.cellName2); 的{​​{1}}。你的错误是在那里的第一行(我假设)。什么是MasterViewController?它不是字符串,您没有将其定义为变量或属性,因此错误。

答案 1 :(得分:0)

您确定代码是在正确的类中吗?并且你在故事板中正确连接了segue?

在DetailViewController代码中,指示segue标识符为“showDetail”。在我看来,你应该在你的MasterViewController中处理它,因为模式通常是MasterViewController负责监视DetailViewController以显示细节。

基本上,如果segue的destinationViewController属性不是MasterViewController的实例,那么segue未正确配置或您的代码位于错误的位置。

如果我弄错了,请发布更多代码。