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