gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:26:29 25 4
gpt4 key购买 nike

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

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

这是 Storyboard的样子

enter image description here

在选择单元格时,我希望它移动到“单一联系人” View 。提取联系人以显示在表格上的代码工作得很好。这是 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''

最佳答案

如果您使用 Storyboard 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");

}

}

关于ios - 从嵌入 TabBarController 和 NavigationController 中的 ViewController 推送新的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20400050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com