說明:記錄下學習IOS的一些瑣碎,有些在現在看起來很簡單幼稚,不過權當學習足跡吧!
1.懶加載(即延遲加載)只有被調用時才初始化,防止資源浪費,需要重寫對象 的get方法,且必須寫成成員變量形式,如_imageData。可以這么寫,如:
1 @PRoperty(nonatomic,strong) NSArray *imageData; 2 3 -(NSArray *)imageData{ //重寫imageData的get方法 4 if(_imageData == nil){ 5 //初始化數據 6 NSMutableDictionary *image1 = [NSMutableDictionary dictionary]; 7 image1[@"icon"] = @"hello"; 8 image1[@"desc"] = @"這是一張圖片的說明"; 9 10 NSMutableDictionary *image2 = [NSMutableDictionary dictionary];11 image2[@"icon"] = @"hello2";12 image2[@"desc"] = @"這是一張圖片的說明2";13 14 //self.imageData = @[image1,image2];//原來的寫法15 _imageData = @[image1,image2]; //懶加載的寫法16 17 }18 return _imageData;19 }
上述get方法不能這樣寫
1 -(NSArray *) imageData{2 if(self.imageData == nil){3 //4 }5 return self.imageData;6 }
因為這樣寫會陷入死循環,self.imageData 調用的就是imageData的get方法,所以得寫成成員變量"_imageData"這種形式的。
2.當數據量較多時,可以把數據存儲在plist文件中,讀取plist文件可以這樣來,固定格式,3行。
//一個bundle就表示一個文件夾,利用mainBundle可以訪問手機的任何資源NSBundle *bundle = [NSBundle mainBundle];//獲取plist文件路徑,并且這種路徑是全路徑,而不單單是通過文件名獲取NSString *path = [bundle pathForResource:@"imageData" ofType:"@"plist"];_imageData = [NSArray arrayWithContentOfFile:path];//一般含File的路徑必須是全路徑,而不是和imageNamed的那樣,只提供名稱就行。
3.出現這種錯誤:
undefined symbols for architecture arm64:
"_OBJC_CLASS_$_BMKMapView", referenced from:"………………
這是程序不支持64位命令,有兩種修改方法
①在target的build settings中的valid Architectures ,將arm64刪除,并將Build Active Architecture Only設為NO,即可。
②另一種是只需修改Architectures的Architectures改為
${ARCHS_STANDARD_32_BIT}即可,原來的為
$(ARCHS_STANDARD)

4.關于UIButton的點擊事件,若將一個btn添加到view里面
[self addSubView:btn];
若該button的坐標位于view之外,那么button就接受不到按鈕事件。其余的類似
5.viewWithTag可以根據tag值很快地獲取到所需要的view,不過對tag的大小有要求,因為tag值較小的,如0——100為蘋果保留所用。
所以我們自定義時需要把tag設的很大才行,如100000.
6.userInteractionEnabled是UIView的屬性,可以設置視圖是否可以接受用戶的事件和消息,是否可以跟用戶交互。若不想,設置為NO即可。
如當一個父視圖中包含兩個子視圖a,b。但b被a覆蓋了,這樣b就不能響應事件,這時若設置
a.userInteractionEnabled = NO;b.userInteractionEnabled = YES;這樣b就可以接收到消息事件了。
7.keyWindow是用來接受鍵盤以及非接觸類的信息,而且每個程序中都只能有一個window是keywindow.
//定義keywindowUIWindow *keyWindow = [[UIapplication sharedApplication] keyWindow];
8.獲取UIButton上的文字,可以用
myButton.titleLabel.text
9.定義可變數組及初始化
1 @property(strong, nonatomic) NSMutableArray *allMedicBtn; 2 3 -(NSMutableArray *) allMedicBtn{ 4 if(!_allMedicBtn){ 5 _allMedicBtn = [NSMutableArray array];//第20條筆記講述這樣寫的原因 6 } 7 return _allMedicBtn; //return self.allMedicBtn 8 //這里_allMedicBtn相當于self.allMedicBtn,這其實調用的是allMedicBtn的get方法,因此這兩種寫法是等價的 9 // self.allMedicBtn 與 _allMedicBtn;10 }
10.[NSMutableArray array]和[[NSMutableArray alloc] init]的區別;
[NSMutableArray array]相當于[[[NSMutableArray alloc] init] autorelease],autorelease的對象有時會在不用的時候已經release了,后面想用時還得retain一次。
11.xcode的快捷鍵
xocde沒有eclipse那樣的花括號成對的提示符,只能雙擊任一花括號,成對的花括號就會以陰影形式出現。
展開、閉合單一方法,快捷鍵是 command + option + 左右鍵,(左鍵閉合,右鍵展開)
要想對所有方法做這些操作,需要加shift,command + option + shift + 左右鍵
12.在普通的view中,一些方法可以加載initWithFrame中,如:
1 -(id)initWithFrame:(CGRect)frame{2 self = [super initWithFrame:frame];3 if (self) {4 //添加選擇器5 [self setupChoiceView];6 }7 return self;8 }
但在tableViewCell中,這些方法最好寫在initWithStyle中,方便復用。如
1 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier2 {3 if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {4 //自定義方法5 [self setupFTEHitorySwitchBtn];6 }7 return self;8 }
13.ios中坐標的定義,當把UIView添加到keyWindow后,view相對的坐標原點就會發生變化,ipad橫屏程序中,默認進入主頁時的home鍵是位于左邊的,此時的坐標原點應該位于右上角(倒過來看還是左上角)。
而假如不是加入到keyWindow中,那么坐標原點還是位于左上角。

而對于keyWindow的定義是在appDelegate中進行的,如
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{2 self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];3 self.window.rootViewController = [[LoginViewController alloc]init];4 [self.window makeKeyAndVisible];5 6 return YES;7 }
對于這種問題,則可以這么解決(這是view中添加view)
①在view的.h文件中,添加新方法。如
1 @class HomeViewController; //為下邊的方法要使用的類提供接口2 @interface MedicineSelectView:UIView3 -(id)initWithFrame:(CGRect) frame andSuperViewController:(HomeViewController *) home;4 @end
②在view的.m文件中添加自定義的方法,原來的方法不再需要。原來的為
1 -(id)initWithFrame:(CGRect)frame{2 self = [super initWithFrame:frame];3 if(self){4 [self setupAddMedicineBtn];5 }6 return self;7 }
此時,上述方法應改為
1 -(id)initWithFrame:(CGRect) frame andSuperViewController:(HomeViewController *) home{ 2 self = [super initWithFrame:frame]; 3 if(self){ 4 self.home = home; //當然還需要在.m中聲明如下 5 /** 6 *@property (weak, nonatomic) UIViewController *home; 7 *因為self.home = home中等號左邊的home是指的property的home,等號右邊的home是方法中的參數 8 */ 9 [self setupAddMedicineBtn];10 }11 return self;12 }
③在view的.m文件中添加該view時也會發生變化。原來是添加到keyWindow內,如
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];[keyWindow addSubView:self.allMedicineView];改變后的方法是將view添加到controller所在的view
[self.home.view addSubview:self.allMedicineView];
④在要將該view添加的controller中,定義該view的大小位置時也需變化。
原來的是
MedicineSelectView *medicineView = [[MedicineSelectView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
改變后的是
MedicineSelectView *medicineView = [[MedicineSelectView alloc] initWithFrame:CGRectMake(0, 0, 300, 44) andSuperViewcontroller:self];//self指的是要添加該view的控制器
14.ipad解決鍵盤遮住文本框的問題,這種方法較簡單,是用來監聽文本框的。如有兩個文本框userName,passWord(都為UITextField),那么可以這樣。
①先對兩個文本框添加監聽事件,這里只以userName為例
1 [userName addTarget:self action:@selector(textFieldBeginEdit:) forControlEvents:UIControlEventEditingDidBegin];2 [userName addTarget:self action:@selector(textFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd];
②添加具體的移動方法
//開始編輯時,整體上移-(void)textFieldBeginEdit:(UITextField *)textField{ [self moveView:-230]; }//結束編輯后,移回原來位置-(void)textFieldEndEdit:(UITextField *)textField{ [self moveView:230];}//view移動的具體方法-(void) moveView:(float)move{ NSTimeInterval animationDuration = 0.3f; CGRect frame = self.view.frame; frame.origin.x += move; //view的X軸上移 self.view.frame = frame; [UIView beginAnimations:@"2" context:nil];// 2是動畫的標識 [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations];}
③點擊其它區域和鍵盤右下角“隱藏鍵盤”那個按鈕時隱藏鍵盤,寫在viewDidLoad里
- (void)viewDidLoad { //點擊其他區域關閉鍵盤 UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyBoard)]; gesture.numberOfTapsRequired = 1; //確認是單擊其他區域,而不是雙擊等其它操作 [self.view addGestureRecognizer:gesture]; //該方法特別重要 //點擊鍵盤右下角“隱藏鍵盤”那個按鈕,關閉鍵盤并且恢復view的位置 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard) name:UIKeyboardWillHideNotification object:nil];}//隱藏鍵盤,并且view恢復初始位置-(void)hideKeyBoard{ [userName resignFirstResponder]; [passWord resignFirstResponder]; [self resumeView];}//恢復view的原來位置-(void)resumeView{ NSTimeInterval animationDuration=0.5f; [UIView beginAnimations:@"3" context:nil]; [UIView setAnimationDuration:animationDuration]; [UIView commitAnimations];}
15.UITableViewCell的重用,比如表格有100行數據,每頁只能顯示10行,則當向下劃時,第11行出現,第1行消失時,為了節約內存,就要將第1行的cell復用到第11行。代碼如下:
+(instancetype) cellWithTableView:(UITableView *) tableView{ static NSString *ID = @"MedicineProcessCell"; MedicineProcessView *cell = [tableView dequeueReusableCellWithIdentifier:ID]; //表示定義一個cell,在tableView的可重用隊列中尋找有MedicineProcessCell標識的UITableViewCell,已進行重用 //如果隊列中有這樣的UITableView,則把它賦值給cell;若沒有,則返回nil給cell if(!cell){ cell = [[MedicineProcessView alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID]; } return cell;}
新聞熱點
疑難解答