1

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

DetailViewController.h

没有什么

细节视图控制器.m

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

主视图控制器.h

@property(nonatomic,strong) NSString *cellName2;

主视图控制器.m

@synthesize cellName2;

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

}

我的问题出在我设置的 detailViewController 中cellName2,我无法设置它,因为它detailViewControllerreceivingViewController. segue他们是设置segue的sendingViewController结束的方法吗?


编辑

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

MasterViewContoller.h

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface MasterViewController : UITableViewController <DetailViewDelegate>

@property(nonatomic,strong) NSString *cellName2;


@end

.m

#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

DetailViewContoller.h

#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

.m

#import "DetailViewController.h"

@interface DetailViewController ()
- (void)configureView;
@end

@implementation DetailViewController

#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }
}

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        self.detailDescriptionLabel.text = [self.detailItem description];
    }
}

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

    //[self.delegate setCellName2:@""];

    [self configureView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"showDetaila"])
    {
        [segue.destinationViewController setCellName2:@"New String"];
    }
}
@end

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

4

2 回答 2

3

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

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

细节视图控制器.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

主视图控制器.h

#import "DetailViewController.h"

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

主视图控制器.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's viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

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

然后删除:

self.cellName2 = cellName;

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

从你MasterViewControllerviewDidLoad. 您的错误在于那里的第一行(我假设)。是什么cellName?它不是字符串,您还没有将其定义为变量或属性,因此出现错误。

于 2013-08-14T14:09:25.323 回答
0

您确定代码在正确的类中吗?并且您在情节提要中正确连接了segues?

在您的 DetailViewController 代码中,您指出 segue 标识符是“showDetail”。在我看来,您应该在 MasterViewController 中处理它,因为该模式通常是 MasterViewController 负责连接到 DetailViewController 以显示详细信息。

基本上,如果 segue 的 destinationViewController 属性不是 MasterViewController 的实例,那么要么没有正确配置 segue,要么你的代码放错了地方。

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

于 2013-08-14T14:04:54.000 回答