pushViewController:详细信息没有推送detailView

时间:2011-10-26 18:37:40

标签: objective-c ios pushviewcontroller

我让我的应用程序完美地导航视图。现在,由于某些我无法弄清楚的原因,我的detailView没有在我的模态视图中呈现pushViewController:detail方法。

我无法弄清楚为什么它不再起作用了。任何帮助,将不胜感激。感谢。

以下是方法:

   - (void)tableView:(UITableView *)tView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


             [tView deselectRowAtIndexPath:indexPath animated:YES];

            if(indexPath.row == 0){
                NSLog(@"hey");
                DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
                detail.delegate = self;
                [[self navigationController] pushViewController:detail animated:YES];
            }

            if(indexPath.row == 1){
                NSLog(@"hey");
                DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
                detail.delegate = self;
                [[self navigationController] pushViewController:detail animated:YES];
            }

            if(indexPath.row == 2){
                NSLog(@"hey");
                DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
                detail.delegate = self;
                [[self navigationController] pushViewController:detail animated:YES];
            }

            if(indexPath.row == 3){
                NSLog(@"hey");
                DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
                detail.delegate = self;
                [[self navigationController] pushViewController:detail animated:YES];
            }

            if(indexPath.row == 4){
                NSLog(@"hey");
                DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
                detail.delegate = self;
                [[self navigationController] pushViewController:detail animated:YES];
            }



        }

这是类代码。我不确定是否需要使用modalView传递一个navigationController:

//
//  ModalView.m
//  DiningLog
//
//  Created by Eric Rea on 10/10/11.
//  Copyright 2011 Avid. All rights reserved.
//

#import "ModalView.h"
#import "DetailView.h"

@implementation ModalView

@synthesize tableView, excersizeName, numSets, time, restTime, dateFormatter, rating, excersizeArray, plistArray, numberWithBool;


-(IBAction) cancel:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
}


-(IBAction) save:(id)sender{

    // get paths from root direcory
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];
    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"ExcersizeList.plist"];

    // check to see if Data.plist exists in documents
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
    {
        // if not in documents, get property list from main bundle
        plistPath = [[NSBundle mainBundle] pathForResource:@"Excersizes" ofType:@"plist"];
    }

    // read property list into memory as an NSData object
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    // convert static property list into dictionary object
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
    if (!temp)
    {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    }
    // assign values
    self.plistArray = [NSMutableArray arrayWithArray:[temp objectForKey:@"Excersizes"]];
    NSLog(@"The contents of plistArray is%@", plistArray);

    // set the variables to the values in the text fields
    self.excersizeArray = [[NSMutableArray alloc] initWithCapacity:4];
    [excersizeArray addObject:excersizeName];
    [excersizeArray addObject:numSets];
    [excersizeArray addObject:time];
    [excersizeArray addObject:restTime];
    [plistArray addObject:excersizeArray];

    // create dictionary with values in UITextFields
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: plistArray, nil] forKeys:[NSArray arrayWithObjects: @"Excersizes", nil]];

    NSString *error = nil;
    // create NSData from dictionary
    // NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    // check is plistData exists
    if(plistDict)
    {
        // write plistData to our Data.plist file
        [plistDict writeToFile:plistPath atomically:YES];
    }
    else
    {
        NSLog(@"Error in saveData: %@", error);
        [error release];
    }

     [self dismissModalViewControllerAnimated:YES];
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tView {

    // Return the number of sections.

    return 1;

}


-(void)setObject:(id)object forNum:(int)num{

    if(num == 0){
        self.excersizeName = object;
        NSLog(@"res %@", self.excersizeName);
    }else if(num == 1){
        self.numSets = object;
        NSLog(@"res %@", self.numSets);
    }else if(num == 2){
        self.time = object;
        NSLog(@"res %@", self.time);
    }else if(num == 3){
        self.restTime = object;
        NSLog(@"res %@", self.restTime);
    }else if(num == 4){
        self.rating = [object floatValue];

    }

    [tableView reloadData];

}


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

    // Return the number of rows in the section.

    return 5;

}





// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
    }

    if(indexPath.row == 0){
        cell.textLabel.text = @"Excersize";
        cell.detailTextLabel.text = excersizeName;
    }
    if(indexPath.row == 1){
        cell.textLabel.text = @"Sets";  
        cell.detailTextLabel.text = numSets;
        }
    if(indexPath.row == 2){
        cell.textLabel.text = @"Time";
        cell.detailTextLabel.text = time;
    }
    if(indexPath.row == 3){
        cell.textLabel.text = @"Rest";
        cell.detailTextLabel.text = restTime;
        }

    if(indexPath.row == 4){
        cell.textLabel.text = @"Rating";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%f",rating];
        }

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        return cell;

}

- (NSDateFormatter *)dateFormatter {    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    }
    return dateFormatter;
}

#pragma mark -

#pragma mark Table view delegate



    - (void)tableView:(UITableView *)tView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


         [tView deselectRowAtIndexPath:indexPath animated:YES];

        if(indexPath.row == 0){
            NSLog(@"hey");
            DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
            detail.delegate = self;
            [[self navigationController] pushViewController:detail animated:YES];
        }

        if(indexPath.row == 1){
            NSLog(@"hey");
            DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
            detail.delegate = self;
            [[self navigationController] pushViewController:detail animated:YES];
        }

        if(indexPath.row == 2){
            NSLog(@"hey");
            DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
            detail.delegate = self;
            [[self navigationController] pushViewController:detail animated:YES];
        }

        if(indexPath.row == 3){
            NSLog(@"hey");
            DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
            detail.delegate = self;
            [[self navigationController] pushViewController:detail animated:YES];
        }

        if(indexPath.row == 4){
            NSLog(@"hey");
            DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
            detail.delegate = self;
            [[self navigationController] pushViewController:detail animated:YES];
        }



    }

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    }
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


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


@end

2 个答案:

答案 0 :(得分:2)

您是否已调试并检查过您的navigationController是否为零?

答案 1 :(得分:0)

我明白了。我不小心删除了rootViewController中的方法insertNewObject。当我重写它时,我忘了添加ModalView * modal = [[ModalView alloc] init];。这就是问题所在。

以下是该方法现在的工作原理:

- (void)insertNewObject {

    ModalView * modal = [[ModalView alloc] init];
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:modal];

    [self presentModalViewController:controller animated:YES];

}