MasterView到DetailView不起作用

时间:2014-12-13 15:38:29

标签: ios objective-c master-detail

我正在使用模板进行MasterView应用程序,但是没有正常工作,当我点击列表的元素时,没有任何内容传递给detailView,有什么问题?

MasterViewController.h:

#import <UIKit/UIKit.h>
#import "u4mjkw_listofmodules.h"

@class DetailViewController;

@interface MasterViewController : UITableViewController{
    NSOperationQueue *queue;
}

@property (strong, nonatomic) DetailViewController *detailViewController;

@property u4mjkw_listofmodules *modules;

@property (nonatomic,strong) IBOutlet UITableView *myTableView;

-(void)updateModules;
@end

MasterViewController.m

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "u4mjkw_listofmodules.h"

@interface MasterViewController ()

@property NSMutableArray *objects;
@end

@implementation MasterViewController
- (void)awakeFromNib {
    [super awakeFromNib];
    self.clearsSelectionOnViewWillAppear = NO;
    self.preferredContentSize = CGSizeMake(320.0, 600.0);
}

- (void)viewDidLoad {
    _modules = [u4mjkw_listofmodules alloc];
    _modules = [_modules init];
     queue = [[NSOperationQueue alloc] init];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        [_modules populateModules:0];
        [_modules order];
    });


    [super viewDidLoad];

    UIRefreshControl * refresh = [[UIRefreshControl alloc]init];

    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh"];

    [refresh addTarget:self action:@selector(refreshModules:) forControlEvents:UIControlEventValueChanged];

    self.refreshControl = refresh;
    self.detailViewController = (DetailViewController *)    [[self.splitViewController.viewControllers lastObject] topViewController];
}

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


#pragma mark - Segues

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSDate *object = self.objects[indexPath.row];
    DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
    [controller setDetailItem:object];
    controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
    controller.navigationItem.leftItemsSupplementBackButton = YES;

}

#pragma mark - Table View

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if(section==0)return [_modules semester1];
    if(section==1)return [_modules semester2];
    return [_modules count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    int i = indexPath.row;
    if(indexPath.section==1) i = i+ [_modules semester1];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    u4mjkw_module *module;
    module = [u4mjkw_module alloc];
    module = [self.modules printModule:i];
    UILabel *label = (UILabel *) [cell viewWithTag:2];
    label.text = [module code];
    UILabel *label1 = (UILabel *) [cell viewWithTag:1];
    label1.text=[module title];
    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) {
        [self.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.
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,18)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,5,tableView.frame.size.width,18)];

    [label setFont:[UIFont boldSystemFontOfSize:12]];
    if(section==0)[label setText:@"Semester 1"];
    if(section==1)[label setText:@"Semester 2"];
    [view addSubview:label];
    [view setBackgroundColor:[UIColor colorWithWhite:0.667f alpha:1.0f]];
    return view;

}
//populate  modules.
- (void)populateModules{

    [_modules populateModules:0];
    [_modules order];

}
//this method will refresh the modules in another thread.
-(void)refreshModules:(UIRefreshControl *)refresh{
    refresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing..."];
    NSLog(@"Refreshing...");


    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                    initWithTarget:self
                                    selector:@selector(updateModules)
                                    object:nil];

    [queue addOperation:operation];

    [_myTableView reloadData];

    [refresh endRefreshing];

}
//update  modules.
- (void)updateModules{
[_modules populateModules:1];
    [_modules order];

}
@end

DetailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

@property (strong, nonatomic) id detailItem;
@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleOfModule;
@property (weak, nonatomic) IBOutlet UILabel *lecturer;
@property (weak, nonatomic) IBOutlet UILabel *webSiteTitle;
@property (strong, nonatomic) IBOutlet UIView *webSiteURL;
@property (weak, nonatomic) IBOutlet UITextView *description;

@end

DetailViewController.m

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
@synthesize titleOfModule, lecturer, webSiteURL,webSiteTitle,description;

#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.titleOfModule.text = [self.detailItem title];
    }
}

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

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

@end

0 个答案:

没有答案