iOS工作笔记(十二)
1.從ControllerA跳往照相UIImagePickerController,只能用presentViewController形式,因?yàn)槎xUIImagePickerController時,
UIImagePickerController *pickerController = [[UIImagePickerController alloc]init]; pickerController.allowsEditing = NO; pickerController.delegate = self; pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;設(shè)置delegate時,若只引入UIImagePickerControllerDelegate,那么會報以下警告:
Assigning to ‘id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>’ from incompatible type ‘AddSightingViewController *const __strong’解決方法是再添加UINavigationControllerDelegate。如下
@interface ControllerA()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>@end所以當(dāng)跳轉(zhuǎn)界面時用
[self.navigationController pushViewController:pickerController animated:YES];會報Pushing a navigation controller is not supported錯誤
引以為戒
?
2.解決A valid provisioning profile for this executable was not found 問題
當(dāng)導(dǎo)入簽名和證書后,真機(jī)調(diào)試仍出現(xiàn)這種問題,那么可以按照以下步驟排查
①先檢查provisioning profile是否導(dǎo)入成功
我用的Xcode版本是7.3.1,在Window——>Devices——>選中運(yùn)行的真機(jī)——>provisioning profiles查看
②檢查 Project 和 Target 中的 Code Signing Identity 是否設(shè)置一樣,必須一樣
?
3.當(dāng)往NSArray中存入基本數(shù)據(jù)類型時,可以用NSNumber,也可以用簡寫
//NSNumber形式 _signTypesArr = [NSArray arrayWithObjects:[NSNumber numberWithInteger:3],[NSNumber numberWithInteger:2], nil]; //簡寫形式 _signTypesArr = [NSArray arrayWithObjects:@(2),@(2),@(2),@(1),@(3),@(3),@(3), nil];但從數(shù)組中往外讀取時,下面這種寫法雖然不報錯,但取的值不對,會取到意想不到的值
NSInteger typeDef = self.signTypesArr[i];正確的是
NSInteger typeDef = [self.signTypesArr[i] integerValue];?
4.NSError轉(zhuǎn)換為NSString
NSError *error= [NSError errorWithDomain:@"" code:0 userInfo:errInfo]; NSString *errorStr = [error localizedDescription];?
5.在有tabbarcontroller的程序中,手動觸發(fā)跳往指定頁面。
雖然tabbarcontroller的代理方法中,有
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {}這樣的方法,但都不能手動調(diào)用。這些是由系統(tǒng)調(diào)用。
如若在選項(xiàng)卡A界面有個按鈕,點(diǎn)擊想跳往選項(xiàng)卡B界面。此時不能用用普通的push和present方法,得用類似于
這樣的寫法。這也是手動觸發(fā)跳往指定頁面的方法。
若有這樣的場景,如在如下的仿微信tabbar中,
點(diǎn)擊前三個tabbar,都能展示相應(yīng)的頁面,在點(diǎn)擊第四個tabbar“我”時,若沒有登錄就需要彈出登錄界面,關(guān)閉登錄界面就跳回到之前點(diǎn)擊的選項(xiàng)卡。如在沒登錄的情況下,先點(diǎn)第二個“通訊錄”,再點(diǎn)第四個“我”,在取消登錄,那么此時界面應(yīng)跳往第二個的“通訊錄”,而不是其它界面。
剛開始的想法是記錄點(diǎn)擊位置,再傳回到第四個tabbar中進(jìn)行處理,但那樣比較麻煩,現(xiàn)在有個簡單方法。
在tabbarcontroller中,
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{NSInteger numIndex = [tabBar.items indexOfObject:item];NSNumber *num = [NSNumber numberWithInteger:numIndex];if (numIndex < 3) {[[NSNotificationCenter defaultCenter] postNotificationName:@"lastSelectedIndex" object:@{@"numIndex":num}];} }將選擇的下標(biāo)通過通知傳出去。然后在第四個“我”中controller的init中接收通知。
// 記錄點(diǎn)擊該選項(xiàng)卡之前最后選擇的下標(biāo) @property(nonatomic,assign) NSInteger numIndex;-(instancetype)init{__weak typeof(self) weakSelf = self;[[NSNotificationCenter defaultCenter] addObserverForName:@"lastSelectedIndex" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {NSNumber *temp = note.object[@"numIndex"];weakSelf.numIndex = [temp integerValue];}];return self; }然后在點(diǎn)擊關(guān)閉登錄界面的方法中執(zhí)行
-(void)clickTheCloseBtn{self.tabBarController.selectedIndex = self.numIndex; }即可實(shí)現(xiàn)該需求。效果如下
?
6.當(dāng)controller的view添加UITapGestureRecognizer后,tableview點(diǎn)擊事件不響應(yīng)的處理
在controller中,有
?
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapWholeView)]; tap.delegate = self; [self.view addGestureRecognizer:tap];?
然后就發(fā)現(xiàn),didSelectRowAtIndexPath方法就不執(zhí)行。因?yàn)槭謩莞采w了cell的點(diǎn)擊方法,此時應(yīng)
實(shí)現(xiàn)UIGestureRecognizerDelegate的代理方法,在手勢點(diǎn)擊tableview時不處理
?
?7.改變pushViewController的push方向
可以實(shí)現(xiàn)類似present的效果。如
SearchGoodsViewController *search = [[SearchGoodsViewController alloc] init]; [self presentViewController:search animated:YES completion:nil];效果是從下到上present
若用push,則可以用動畫來解決
?
8.uitableview,uicollectionview,uiscrollview回到頂部
UITableView, UICollectionView都繼承自UIScrollView,所以可以使用UIScrollView的方法,設(shè)置顯示內(nèi)容的偏移量。有兩種寫法
[_myTableView setContentOffset:CGPointZero animated:YES]; [_myTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];若想讓tableview,collectionview隨著cell的點(diǎn)擊而移動,而不是手動移動,那么可以在點(diǎn)擊方法里設(shè)置
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{[_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; }?
?********************************** 2016-07-15更新?**********************************?
上述被刪除的部分是有問題的,假如animated為YES的話,有時候tableview不會返回到正確的位置。因此需要設(shè)置animated為NO。更準(zhǔn)確的寫法為
[_myTableView setContentOffset:CGPointZero animated:NO]; [_myTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];關(guān)于這部分的討論地址
http://stackoverflow.com/questions/724892/uitableview-scroll-to-the-top
轉(zhuǎn)載于:https://www.cnblogs.com/Apologize/p/5584890.html
總結(jié)
以上是生活随笔為你收集整理的iOS工作笔记(十二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 东方红1404变速箱响,倒挡紧
- 下一篇: react服务端/客户端,同构代码心得