作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开始使用我的应用程序中的方法创建单例,这些方法需要被不同的 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/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!