1

我的问题 - 我想从 DetailView 中的 TableView 传递信息,但在self.detailViewController.detailItem的某处没有传递任何值。

那是:

self.detailViewController.detailItem =@"123";

NSString *sss = self.detailViewController.detailItem;

NSLog(@"%@",  sss);

在 NSLog 输出中(空)

这是我的源代码:

DetailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController <UISplitViewControllerDelegate>

@property id detailItem;
@property id TitleOfDetail;

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

@end

细节视图控制器.m

#import "DetailViewController.h"

@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end

@implementation DetailViewController




#pragma mark - Managing the detail item


-(void)setTitleOfDetail:(id)newTitleOfDetail {

    NSLog(@"123");

    if (_TitleOfDetail != newTitleOfDetail) {
        _TitleOfDetail = newTitleOfDetail;
        // Update the view.
        [self configureView];
    }
}


-(void)setDetailItem:(id)newDetailItem
{
    NSLog(@"123");

    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

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

    if (self.masterPopoverController != nil) {
        self.title = self.TitleOfDetail;
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

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

    NSLog(@"1%@", self.TitleOfDetail);
    NSLog(@"2%@", [self.detailItem description]);

    if (self.TitleOfDetail) {
        self.title = self.TitleOfDetail;
    }


    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 configureView];
}
…

我的班级.h

@class DetailViewController;

@interface myclass : UITableViewController 

@property DetailViewController *detailViewController;

@end

我的班级.m

#import "choiseAvtoController.h"
#import "DetailViewController.h"

@interface myclass ()
@end

@implementation myclass

@synthesize detailViewController;

…

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.detailViewController.detailItem =@"123";

NSString *sss = self.detailViewController.detailItem;

NSLog(@"%@",  sss);
}

可能是什么问题以及如何解决?

PS对不起我的英语,我只是在学习))

4

1 回答 1

1

由于您使用的是故事板,因此您应该在 segue 上打开细节控制器。

在故事板中查找 segue 标识符(例如,它是OpenDetail),然后将以下代码添加到您的初始控制器:

-(void)prepareForSegue:(UIStoryboardPopoverSegue*)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"OpenDetail"]) {
        DetailViewController *dest = segue.destinationViewController;
        dest.detailItem =@"123";
    }
}
于 2012-12-09T20:32:07.290 回答