gpt4 book ai didi

iphone - Objective-C 通讯录

转载 作者:搜寻专家 更新时间:2023-10-30 20:27:40 28 4
gpt4 key购买 nike

我有两个继承自 UIViewController 的 Objective-C 类,我正在尝试一种不同的方法来学习如何与 iPhone 的地址簿进行交互。 example Apple 假设一切都在一个类中,但这不是我需要的方式。我的目标是在选择一个人后关闭地址簿 View 。请看一下,让我知道如何在没有 CallerClass 实现 ABPeoplePickerNavigationControllerDelegate 的情况下完成此操作。谢谢!

-- 编辑--

它似乎归结为 [self dismissModalViewControllerAnimated:YES];在 CalleeClass.m 中没有任何影响。我似乎仍然无法通过此命令获得关闭地址簿的 react 。

调用者类.m

#import "CallerClass.h"

@implementation CallerClass
- (IBAction)openAddressBook {
CalleeClass *cc = [[CalleeClass alloc] init];
[self presentModalViewController:[cc doIt] animated:YES];
}

被调用类.h

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

@interface CalleeClass : UIViewController <ABPeoplePickerNavigationControllerDelegate> {
NSString *name;
}

-(ABPeoplePickerNavigationController *)doIt;

@property (nontoxic, retain) NSString *name;

@end

被调用类.m

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "CalleeClass.h"

@implementation CalleeClass
@synthesize name;

…(在所列内容之外的默认 ABPeoplePickerNaviationControllerDelegate 实现)

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {}
return self;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
self.name = (NSString *)ABRecordCopyValue(person,kABPersonAddressProperty);

[self dismissModalViewControllerAnimated:YES];
return NO;
}

-(ABPeoplePickerNavigationController *)doIt {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
return picker;
}

@end

最佳答案

如果问题是,如您所说,如果从 CalleeClass 调用,[self dismissModalViewControllerAnimated:YES] 没有效果,这是因为 dismissModalViewControllerAnimated: 必须在呈现 View Controller 上调用(即,您调用 presentModalViewController:Animated: 的 View Controller 。因为您没有对 CallerClass 的引用CalleeClass 中的实例,这不起作用。

幸运的是,dismissModalViewControllerAnimated: 的文档指出:

If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller.

所以这应该可行:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
self.name = (NSString *)ABRecordCopyValue(person,kABPersonAddressProperty);
[peoplePicker dismissModalViewControllerAnimated:YES];
return NO;
}

关于iphone - Objective-C 通讯录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1609519/

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