說明:記錄下學(xué)習(xí)IOS的一些瑣碎,有些在現(xiàn)在看起來很簡單幼稚,不過權(quán)當(dāng)學(xué)習(xí)足跡吧!
1.xib開發(fā)ipad使用橫屏,可以在xib的view設(shè)置orientation為landscape,portrait是豎屏。
2.在.m文件中聲明UITextField后,若想獲取。比如想獲取pwd
@PRoperty(nonatomic,weak) UITextField *pwd;
那么可以用 self.pwd.text獲取。
_pwd.text;// 這也可以獲取到。因?yàn)閟elf.view其實(shí)調(diào)用的是pwd的get方法。就是
1 -(UITextField *)pwd{2 return _pwd;3 }
3.字符串比較,看是否相等
1 NSString *str1 = self.pwdField.text;2 if([str1 isEqualToString:@"123"]){3 //其它操作4 }
4.新建一個(gè)項(xiàng)目,若不想使用自帶的viewController和storyboard,刪除之,那么在新建xib文件后容易出現(xiàn)問題就是程序運(yùn)行后主界面是空白的。這時(shí)可以這么來解決,就是自己定義需要現(xiàn)實(shí)的view。
①在Supporting Files文件夾,編輯plist文件。將Main storyboard file base name置空(因?yàn)閟toryboard文件已刪除)。

②在Appdelegate.m的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中添加代碼。如想展示LoginViewController,那么可以這樣寫。固定格式
1 self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];2 self.window.rootViewController = [[LoginViewController alloc]init];3 [self.window makeKeyAndVisible];//讓視圖可見,并顯示在屏幕上
5.使用xib文件連線時(shí),必須把view也連起來,否則就是沒有畫面的程序,切記。
6.顯示控件時(shí),需要用以下代碼。如:
1 [self.view addSubview:btn1];2 [self.view addSubview:btn2];按鈕添加事件時(shí),可以用
1 [btn1 addTarget:self action:@selector(btnPressed:) forControllerEvents:UIControlEventTouchUpInside];2 -(void)btnPressed:(id) sender{3 UIButton *btn = (UIButton *)sender;4 //按鈕的事件代碼5 }當(dāng)然也有如下形式,最好使用上邊的。(id)sender這個(gè)參數(shù)指button本身,
1 -(void) login{ //該函數(shù)沒有參數(shù),容易出錯(cuò)2 //事件3 }
7.一般不這樣定義button
1 CGRect frame4 = CGRectMake(430, 200, 200, 100);2 UIButton *loginBtn = [[UIButton alloc]initWithFrame:frame4];一般是這樣來定義
1 loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];2 loginBtn.frame = frame4;
8.設(shè)置根控制器,需要在AppDelegate中寫。一般這種格式是固定的,需要在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{}
中寫。如:
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 2 //必須寫在最前邊,否則視圖不出現(xiàn) 3 self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]]; 4 //若要顯示其它東西,如百度地圖,那么也要聲明 5 self.mapManager = [[BMKManager alloc]init];//不想顯示時(shí)可以刪去 6 7 //定義MainViewController為根控制器 8 UINavigationController *controller = [UINavigationController alloc]initWithRootViewController:[[MainViewController alloc]init]]; 9 self.window.rootViewController = controller;10 [self.window makeKeyAndVisible];11 self.window.backgroundColor = [UIColor whiteColor];12 return YES;13 14 }若不想使用UINavigationController,那么可以直接定義
self.window.rootViewController = [[LoginViewController alloc]init];相當(dāng)于把定義根控制器的那行代碼刪除即可。
然后對(duì)應(yīng)的,頁面之間的跳轉(zhuǎn)有兩種方法。
[self presentViewController:mainView animated:YES completion:nil];//view自帶的屬性[self.navigationController pushViewController:mainView animated:true];
//必須在AppDelegate設(shè)置UINavigationController,否則pushViewController無效
//并且使用pushViewController后,在B頁面,會(huì)自帶一個(gè)“Back”按鈕返回上一頁。
9.程序加載時(shí)首頁展現(xiàn)的內(nèi)容是在下邊方法實(shí)現(xiàn)的,并且[super viewDidLoad]不能省略。
1 -(void)viewDidLoad{2 [super viewDidLoad];3 //其它代碼 4 }
10.@property屬性的區(qū)別
weak:一般用于UI組件,如UIButton,UILabel,UITextField等。
strong:用于一般對(duì)象,如數(shù)組等
copy:用于NSString
assign:用于基本類型,如int、BOOL等
在xib和純代碼中是有區(qū)別的,xib中UI組件用weak,subViews數(shù)組會(huì)強(qiáng)引用這些組件;純代碼寫時(shí),就得用strong屬性了,否則會(huì)報(bào)
Assigning retained object to weak variable; object will be released after assignment
切記。
NSArray和NSDictionary的用法舉例,字典本身就屬于鍵值對(duì),其中的key為icon和desc,不同圖片的key對(duì)應(yīng)不同的值。
1 @property(nonatomic,strong)NSArray *imageData; 2 NSMutableDictionary *image1 = [NSMutableDictionary dictionary]; 3 image1[@"icon"] = @"hello"; //屬于鍵值對(duì)形式 4 image1[@"desc"] = @"這是一張圖片的說明"; 5 6 NSMutableDictionary *image2 = [NSMutableDictionary dictionary]; 7 image2[@"icon"] = @"hello2"; 8 image2[@"desc"] = @"這是一張圖片的說明2"; 9 10 //快速的創(chuàng)建數(shù)組11 self.imageData = @[image1,image2];當(dāng)程序中使用較多的key,為了防止把key拼錯(cuò),可以定義宏常量,如
#define IconKey @"icon"以后用到@"icon"時(shí),可以用IconKey直接代替
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注