单例模式代码如下:
#import "TRclas.h"
static TRclas *share = nil;
@implementation TRclas
+(TRclas *)defaultNum{
if (!share)
{
share=[[TRclas alloc]init];
}
return share;
}
@end.h文件代码
#import <Foundation/Foundation.h>
@interface TRclas : NSObject
@property(nonatomic ,assign)NSUInteger num;
+(TRclas *)defaultNum;
@end#import "TRAViewController.h"
#import "TRBViewController.h"
#import "TRclas.h"
@interface TRAViewController ()
@property (weak, nonatomic) IBOutlet UIStepper *stepper;
@property(nonatomic,strong)TRBViewController *svc;
@end
@implementation TRAViewController
- (IBAction)btnA:(UIButton *)sender {
[TRclas defaultNum].num = (int)self.stepper.value;
self.svc.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentViewController:self.svc animated:YES completion:nil];
}-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.stepper.value=[TRclas defaultNum].num;
}
本文介绍了一段Objective-C实现的单例模式代码,并解释了其工作原理和使用方法。通过实例演示了如何创建和使用单例对象,以确保在应用程序中全局唯一地访问某个对象。
128

被折叠的 条评论
为什么被折叠?



