iOS 7 statusbar 需要知道的

最近在用sharesdk的过程遇到一个很奇怪问题:http://bbs.mob.com/thread-317-1-1.html
点分享按钮时,弹出modal view会把statusbar从白色改成黑色,几乎把http://stackoverflow.com/上关于statusbar的帖子都看完了,还是没有解决。
只能自己解决,从两方面入手
1、自己写个modal view,同样方式跳出,会不会把statusbar改变
2、和sharesdk的人确认,是否对navigationbar进行修改,把案列代码发给sharesdk

从上面帖子中,可以发现,自己写modal view没有改变statusbar,那就是sharesdk的问题?把sharesdk的demo拿下来一跑,奇怪了,尽量正常的,测底无解了?

sharesdk的开发同学联系我说,他们新建project后,把我发给他们代码import过去后,是没有问题的,但就是我发给们的project还是有问题。从这句话中,我发现有一点,那就是plist文件被新建了,在本地把原来project中的plist文件重建后,app果然正常了,但新的plist和老的plist没啥区别。

这里强调一下statusbar需要知道点:
1、plist中设置View controller-based status bar appearance = YES(默认就是为YES,VC控制statusbar隐藏与样式);preferredStatusBarStyle,可以让用户指定状态栏风格。但这个方法只有在ViewController不包含在UINavigationController中时才起作用。但我们在ViewController时一般都和UINavigationController一起使用。

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

老外提供解决方法:http://mythodeia.wordpress.com/2014/05/09/view-controller-based-status-bar-appearance

2、View controller-based status bar appearance=NO(对整个应用statusbar做统一处理)
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];

ios状态栏颜色问题解决方案

http://stackoverflow.com/questions/17678881/how-to-change-status-bar-text-color-in-ios-7上有很多解决方案,但一直不成功,后来仔细看了下面评论,问题解决。

http://stackoverflow.com/questions/19022210/preferredstatusbarstyle-isnt-called/19513714#19513714

The UINavigationController does not forward on preferredStatusBarStyle calls to its child view controllers. Instead it manages its own state – as it should, it is drawing at the top of the screen where the status bar lives and so should be responsible for it. Therefor implementing preferredStatusBarStyle in your VCs within a nav controller will do nothing – they will never be called.

The trick is what the UINavigationController uses to decide what to return for UIStatusBarStyleDefault or UIStatusBarStyleLightContent. It bases this on it’s UINavigationBar.barStyle. The default (UIBarStyleDefault) results in the dark foreground UIStatusBarStyleDefault status bar. And UIBarStyleBlack will give a UIStatusBarStyleLightContent status bar.

If you want UIStatusBarStyleLightContent on a UINavigationController use:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

不务正业之OC(十五)–代理设计模式

什么是代理设计模式:两个对象相互协作,完成共同的目标。
比如:A需要开发一个app,但又不会,把这个需求发到网上去,这时B看到A的需求,B说我能完成这个任务,B接到这个任务并完成后,通知A。

特点:
1、减小对象之间的耦合度
2、无需子类化

代理设计模式按书上例子写一下:Person实例要找房子,但Person实例没有这个能力,那就委托给Agent类去找房子,找到房子后,和Person实例说,我找到房子
Person类、Agent类、FindApartment协议

 
//  Person.h 
 
#import <Foundation/Foundation.h>
#import "FindApartment.h"
 
@interface Person : NSObject
{
@private
    NSString *_name;
    id <FindApartment> delegate;
}
 
@property(nonatomic,copy) NSString *name;
@property(nonatomic,assign) id<FindApartment> delegate;
//初始化Person对象name及委托人(找谁来找房子)
- (id) initWithName:(NSString *)name withDelegate:(id <FindApartment>) delegate;
//Person对象需求
- (void) wantToFindApartment;
 
@end
 
//  Person.m
 
#import "Person.h"
 
@implementation Person
 
@synthesize name = _name;
@synthesize delegate = _delegate;
 
- (id) initWithName:(NSString *)name withDelegate:(id <FindApartment>) delegate{
 
    if (self = [super init]) {
        self.name = name;
        self.delegate = delegate;
    }
    return self;
}
 
- (void) wantToFindApartment{
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(startFindApartment:) userInfo:nil repeats:YES];
}
 
- (void)startFindApartment:(NSTimer *) timer{
    [self.delegate findApartment];
 
}
 
@end
 
//  Agent.h
 
#import <Foundation/Foundation.h>
#import "FindApartment.h"
 
@interface Agent : NSObject<FindApartment>
 
@end
 
//  Agent.m
 
#import "Agent.h"
//委托人需要实现<FindApartment>中定义方法,Agent有这个能力来找房子
@implementation Agent
- (void)findApartment{
    NSLog(@"find");
}
 
@end
 
//  FindApartment.h
 
#import <Foundation/Foundation.h>
//协议定义
@protocol FindApartment <NSObject>
 
- (void)findApartment;
 
@end
 
 
//  main.m
//  Delegate
 
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Agent.h"
 
int main(int argc, const char * argv[])
{
 
    @autoreleasepool {
        Agent *agent = [[Agent alloc] init];
        //委托给agent来找房子
        Person *jack = [[Person alloc] initWithName:@"jack" withDelegate:agent];
        //调用wantToFindApartment会触发定时器,每隔2s钟调用delegate(这个delegate就是agent)会调用findApartment方法
        [jack wantToFindApartment];
 
        [[NSRunLoop currentRunLoop] run];
 
    }
    return 0;
}

不务正业之OC(十三)–protocol

协议:类似一个类的接口,没有父类,不能定义实例变量;定义方法,需要被另外类实现
使用场合:
1、需要别的类实现方法
2、声明未知类的接口
3、两类之间进行通讯

两关键词
@required
@optional

 
//创建协议
//  MyProtocol.h
 
#import <Foundation/Foundation.h>
 
@protocol MyProtocol <NSObject>
 
@required
- (void) requiredMethod;
 
@optional
- (void) optionalMethod;
 
 
//  Person.h
 
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
 
@interface Person : NSObject <MyProtocol>
 
@end
 
//  Person.m
 
#import "Person.h"
 
@implementation Person
- (void) requiredMethod{
    NSLog(@"must require!");
}
 
- (void) optionalMethod{
    NSLog(@"may optional!");
}
@end
 
//  main.m
 
#import <Foundation/Foundation.h>
#import "Person.h"
 
int main(int argc, const char * argv[])
{
 
    @autoreleasepool {
        Person *person = [[Person alloc] init];
        [person requiredMethod];
        [person optionalMethod];
    }
    return 0;
}

不务正业之OC(十二) 单例设计模式

单例设计模式:只能创建一个实例

什么时候创建单例:
1、单例对象作为全局数据,可以为其他对象访问
2、如果创建对象时,需要init要做很多事情,那么创建单例可以提高性能

创建单例步骤:
1、创建一个对象的静态实例,初始化为nil
2、当类的实例为nil时,创建类工厂方法
3、覆盖allocWithZone方法,alloc时不会产生新对象
4、实现NSCopying协议,覆盖release、autorelease、retain、retainCount方法,确保单例
5、多线程情况下,@synchronized,保证单例正确初始化

//  Lijie.h
//  Single
 
#import <Foundation/Foundation.h>
 
@interface Lijie : NSObject<NSCopying>
 
@property(nonatomic,retain) NSNumber *age;
@property(nonatomic,retain) NSString *sex;
 
+ (id)sharePerson;
 
@end
 
//  Lijie.m
//  Single
 
#import "Lijie.h"
 
static Lijie *singleLijie = nil;
 
@implementation Lijie
 
+ (id)sharePerson{
    @synchronized(self){
        if (singleLijie == nil) {
            singleLijie = [[[self class] alloc] init];
        }
        return singleLijie;
    }
}
 
+ (id) allocWithZone:(NSZone *)zone{
    if (singleLijie == nil) {
        singleLijie = [super allocWithZone:zone];
    }
    return singleLijie;
}
 
- (id)copyWithZone:(NSZone *)zone{
    return singleLijie;
}
 
- (id)retain{
    return singleLijie;
}
 
- (oneway void)release{
}
 
- (id)autorelease{
    return singleLijie;
}
 
- (NSUInteger) retainCount{
    return UINT_MAX;
}
 
 
@end
 
//  main.m
//  Single
 
#import <Foundation/Foundation.h>
#import "Lijie.h"
 
int main(int argc, const char * argv[])
{
 
    @autoreleasepool {
        Lijie *lijie = [Lijie sharePerson];
 
        Lijie *lijie1 = [[Lijie alloc] init];
        Lijie *lijie2 = [Lijie sharePerson];
        Lijie *lijie3 = [lijie1 copy];
 
        [lijie release];
        [lijie release];
        [lijie release];
        [lijie release];
        [lijie release];
        NSLog(@"");//这里搞个breakpoint可以观察lijie1、lijie2、lijie3三个对象地址
    }
    return 0;
}

1