gpt4 book ai didi

ios - 从联系人选择器中创建单例

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

我开始使用我的应用程序中的方法创建单例,这些方法需要被不同的 viewController 重用。

我这里有一个方法可以访问用户的联系人,并允许他们通过 SMS 发送消息。这工作得很好,我现在想把代码变成一个可以被其他 viewController 访问的单例。

到目前为止,这是我的代码。我不确定如何开始。任何建议将不胜感激!

发送短信.h

#import <UIKit/UIKit.h>
#import "SMContactsSelector.h"
#import <MessageUI/MessageUI.h>

@interface SendSMS : UIViewController

- (IBAction)button:(id)sender;

@end

发送短信.m

#import "SendSMS.h"

@interface SendSMS ()

@end

@implementation SendSMS

NSMutableArray *phoneArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated {

NSString *phoneString = [[NSUserDefaults standardUserDefaults]
stringForKey:@"phoneString"];

if (!([phoneString isEqualToString:@""])) {

[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"phoneString"];
[[NSUserDefaults standardUserDefaults] synchronize];

//check if the device can send text messages
if(![MFMessageComposeViewController canSendText]) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Your device cannot send text messages" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}

//set receipients
NSArray *recipients = [phoneString componentsSeparatedByString:@","];

//set message text
NSString *message = @"message";
MFMessageComposeViewController *messageController =
[[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setRecipients:recipients];
[messageController setBody:message];

[self presentViewController:messageController animated:YES completion:nil];
}
}

- (IBAction)button:(id)sender {

SMContactsSelector *controller = [[SMContactsSelector alloc]
initWithNibName:@"SMContactsSelector" bundle:nil];
controller.delegate = self;
controller.requestData = DATA_CONTACT_TELEPHONE; // DATA_CONTACT_ID
DATA_CONTACT_EMAIL , DATA_CONTACT_TELEPHONE
controller.showModal = YES; //Mandatory: YES or NO
controller.showCheckButton = YES; //Mandatory: YES or NO

[self presentModalViewController:controller animated:YES];
}

- (void)numberOfRowsSelected:(NSInteger)numberRows withData:(NSArray *)data
andDataType:(DATA_CONTACT)type {

if (type == DATA_CONTACT_TELEPHONE) {

for (int i = 0; i < [data count]; i++) {

NSString *str = [data objectAtIndex:i];
str = [str reformatTelephone];
[phoneArray addObject:str];

NSString *phoneStringAppended1 = [[NSUserDefaults standardUserDefaults]
stringForKey:@"phoneString"];
NSString *phoneStringAppended2 = [phoneStringAppended1
stringByAppendingString: str];
NSString *phoneString = [phoneStringAppended2 stringByAppendingString:@",
"];
[[NSUserDefaults standardUserDefaults] setObject:phoneString
forKey:@"phoneString"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
}

#pragma mark - MFMailComposeViewControllerDelegate methods
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult) result {
switch (result) {
case MessageComposeResultCancelled: break;
case MessageComposeResultFailed: break;
case MessageComposeResultSent: break;
default: break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

@end

最佳答案

做一个 viewcontroller 单例是不行的。

将您需要的代码放在一个新类中,该类应该是单例的并且可以被所有 View Controller 访问。

我给你一个单例模式

SmsManager.h

#import <foundation/Foundation.h>

@interface SmsManager : NSObject {
NSString *someProperty;
}

@property (nonatomic, retain) NSString *someProperty;

+ (id)sharedManager;

@end

短信管理器.m

#import "SmsManager.h"

@implementation SmsManager

@synthesize someProperty;

#pragma mark Singleton Methods

+ (id)sharedManager {
static MyManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}

- (id)init {
if (self = [super init]) {
// initialization
}
return self;
}
@end

只需调用 SmsManager *smsManager = [SmsManager sharedInstance]; 任何你需要的地方

关于ios - 从联系人选择器中创建单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25904415/

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