Last active
August 29, 2015 14:16
-
-
Save goldcoast/df7476f0853cac8ea127 to your computer and use it in GitHub Desktop.
OC自定义构造方法,初始化私有变量
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// human.h: | |
#import <Foundation/Foundation.h> | |
@interface Human : NSObject | |
{ | |
int age; | |
NSString *name; | |
} | |
-(id)initWithAge:(int)a Name:(NSString *)n; | |
-(int)age; | |
-(NSString *)name; | |
@end | |
// human.m: | |
#import "Human.h" | |
@implementation Human | |
-(id)init | |
{ | |
if(self=[super init]) | |
{ | |
age=20; | |
name=@"holy"; | |
} | |
return self; | |
} | |
-(id)initWithAge:(int)a Name:(NSString *)n | |
{ | |
if (self=[super init]) { | |
age=a; | |
[name release]; | |
name=[n copy]; | |
} | |
return self; | |
} | |
-(int)age | |
{ | |
return age; | |
} | |
-(NSString *)name | |
{ | |
return name; | |
} | |
@end | |
//main.m: | |
#import <Foundation/Foundation.h> | |
#import "Human.h" | |
int main(int argc, const char * argv[]) | |
{ | |
NSAutoreleasePool *pool; | |
pool =[[NSAutoreleasePool alloc] init]; | |
Human * human1 = [[Human alloc] init]; | |
NSLog(@"名字%@,年龄%d",[human1 name],[human1 age]); | |
Human * human2 = [[Human alloc] initWithAge:100 Name:@"GOD"]; | |
NSLog(@"名字%@,年龄%d",[human2 name],[human2 age]); | |
[human1 release]; | |
[human2 release]; | |
[pool release];//相当于对池中每个对象执行了一次release; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GuideViewController.h | |
#import <UIKit/UIKit.h> | |
@interface ViewController : UIViewController <UIScrollViewDelegate> | |
{ | |
@private BOOL hideFlag; | |
} | |
@end | |
/*-----------------华丽的分隔线----------------------*/ | |
//GuideViewController.m | |
@implementation ViewController | |
- (id) initWithNoButton:(BOOL) theHideButton | |
{ | |
hideFlag = theHideFlag; | |
return self; | |
} | |
//some method will invoke hideFlag | |
-(void)someMethod | |
{ | |
....... | |
if (!hideFlag) { | |
[self initBtn]; | |
} | |
} | |
@end | |
/*-----------------华丽的分隔线----------------------*/ | |
//some othe class invoke above class | |
@implementation otherViewController | |
-(void) methodA | |
{ | |
.... | |
ViewController *viewController = [[ViewController alloc] initWithNoButton:YES]; | |
//这个很重要,当在navigationController pushViewController时,被pull的controller如果是一个UIScrollView则一定要加这个,不然 | |
//1.图片上下会动到边界外。 2.导航回到上一视图时程序会崩溃 | |
viewController.automaticallyAdjustsScrollViewInsets = NO; | |
.... | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment