从嵌入在TabBarController和NavigationController中的ViewController推送新的ViewController

时间:2013-12-05 12:27:11

标签: ios objective-c uitableview

我有一个标签栏控制器,其中嵌入了导航控制器,然后是一个视图控制器。在该视图控制器中,我有一个表,当用户选择一行时,我正在尝试显示一个新的视图控制器。

我正在尝试构建联系人页面(引入iOS联系信息)并显示它。

以下是StoryBoard的样子

enter image description here

在单元格选择上,我希望它移动到单一联系人视图。 拉入要在桌面上显示的联系人的代码工作得很好。这是PublicContactsViewController类

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <Parse/Parse.h>
#import <iAd/iAd.h>
#import "SingleContactViewController.h"
#import "Person.h" 

@interface PublicContactsViewController : UIViewController <ADBannerViewDelegate,UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet ADBannerView *banner;

@end

#import "PublicContactsViewController.h"

@interface PublicContactsViewController ()
@property (nonatomic, strong) NSMutableArray *tableData;
@end

@implementation PublicContactsViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    _banner.delegate = self;
 //   self.title = @"Public Contacts";
    self.tableData = [[NSMutableArray alloc]init];
    [self getPersonOutOfAddressBook];

}

#pragma mark - Address Book Methods

- (void)getPersonOutOfAddressBook
{
    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    if (addressBook != nil){
        NSLog(@"getPersonOutOfAddressBook - addressBook successfull!.");

        NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
        NSUInteger i = 0;
        for (i = 0; i < [allContacts count]; i++){
            Person *person = [[Person alloc] init];

            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
            NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
            NSString *lastName =  (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
            NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName, lastName];

            person.firstName = firstName;
            person.lastName = lastName;
            person.fullName = fullName;

            //email
            ABMultiValueRef emails = ABRecordCopyValue(contactPerson,
                                                       kABPersonEmailProperty);
            NSUInteger j = 0;
            for (j = 0; j < ABMultiValueGetCount(emails); j++){
                NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
                if (j == 0){
                    person.homeEmail = email;                 
                    NSLog(@"person.homeEmail = %@ ", person.homeEmail);                 
                }                              
                else if (j==1)                     
                    person.workEmail = email;
                    NSLog(@"person.workEmail = %@ ", person.workEmail);
            }           

            [self.tableData addObject:person];         
        }     
    }          

    CFRelease(addressBook); 
}


#pragma mark - Table View Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Identifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier];
    }
    Person *person = [self.tableData objectAtIndex:indexPath.row];

    cell.textLabel.text = person.fullName;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Cell was clicked");
    Person *person = [self.tableData objectAtIndex:indexPath.row];
    NSLog(@"Person selected: %@",person);
    SingleContactViewController *singleContactViewController = [[SingleContactViewController alloc] initWithPerson:person];
    NSLog(@"created singlecontactviewcontroller");

    [self.navigationController pushViewController:singleContactViewController animated:YES];
}

#pragma mark iAd Delegate Methods

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    // Gets called when iAds is loaded.
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    // Set iAd to visible, creates nice fade-in effect
    [banner setAlpha:1];
    [UIView commitAnimations];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    // Gets called when iAds can't be loaded (no internet connection).
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    // Set iAd to visible, creates nice fade-in effect
    [banner setAlpha:0];
    [UIView commitAnimations];
}



@end

以下是我点击单元格时收到的错误

2013-12-05 07:25:35.186 ConfidentialContacts[21902:70b] Cell was clicked
2013-12-05 07:25:35.187 ConfidentialContacts[21902:70b] Person selected: <Person: 0xad670c0>
2013-12-05 07:25:35.187 ConfidentialContacts[21902:70b] created singlecontactviewcontroller
2013-12-05 07:25:35.225 ConfidentialContacts[21902:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/david/Library/Application Support/iPhone Simulator/7.0.3/Applications/7DCB2344-3497-4CAF-BA47-8E80E6D1FBB6/CustomContacts> (loaded)' with name 'SingleContactViewController''

2 个答案:

答案 0 :(得分:1)

如果您使用故事板segue,那么最好使用prepareForSegue:方法并传递person对象。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Cell was clicked");
    [self performSegueWithIdentifier:@"yourSegueIdentifier" sender:self];

}

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

         NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
         Person *person = [self.tableData objectAtIndex:indexPath.row];
         NSLog(@"Person selected: %@", person);
         SingleContactViewController *singleContactViewController = segue.destinationViewController;
         singleContactViewController.person = person; // create person object as a property in SingleContactViewController and assign the person object.
    NSLog(@"created singlecontactviewcontroller");

   } 

}

答案 1 :(得分:0)

我认为你不能把新的控制器推到现有的控制器上。只是转向新观点。这会将视图推送到viewController的堆栈。