IOS開發基礎知識--片段35,ios基礎知識--35

來源:互聯網
上載者:User

IOS開發基礎知識--片段35,ios基礎知識--35

1:iOS視圖控制對象生命週期

 

init-初始化程式

viewDidLoad-載入視圖

viewWillAppear-UIViewController對象的視圖即將加入視窗時調用;

viewDidApper-UIViewController對象的視圖已經加入到視窗時調用;

viewWillDisappear-UIViewController對象的視圖即將消失、被覆蓋或是隱藏時調用;

viewDidDisappear-UIViewController對象的視圖已經消失、被覆蓋或是隱藏時調用;

 

執行時間順序

15:51:44.811inHyron[483:b903] init

15:51:54.081inHyron[483:b903] viewDidLoad

15:51:54.082inHyron[483:b903] viewVillAppear

15:51:54.084 inHyron[483:b903] viewDidAppear

很明顯,先執行init、然後執行viewDidLoad,然後是viewWillAppear最後是viewDidAppear,這樣視圖就建立好了,當視圖消失或者被覆蓋的時候:

15:54:14.557inHyron[483:b903] viewWillDisappear

15:54:14.558inHyron[483:b903] viewDidDisappear

這樣一來視圖就消失了

 

2:初始化一個有預設值執行個體類

userManager.h檔案內容:#import <Foundation/Foundation.h>@interface userManager : NSObject@property(nonatomic,copy)NSString *userName;@property(nonatomic,copy)NSString *passWord;@property (assign, nonatomic) NSInteger type;+(userManager *)userManagerWithType:(NSInteger)type;-(NSString *)toGetParams;-(void)configWithObj:(userManager *)userManagerInfo;@enduserManager.m檔案內容:#import "userManager.h"@implementation userManager- (instancetype)init{    self = [super init];    if (self) {        _userName=@"root";        _passWord=@"123456";        _type = 0;    }    return self;}+(userManager *)userManagerWithType:(NSInteger)type{    userManager *myManager=[[userManager alloc]init];    myManager.type=type;    return myManager;}-(void)configWithObj:(userManager *)userManagerInfo{    self.userName=userManagerInfo.userName;    self.passWord=userManagerInfo.passWord;    self.type=userManagerInfo.type;}-(NSString *)toGetParams{    return [NSString stringWithFormat:@"目前使用者:%@ 密碼:%@",self.userName,self.passWord];}@end調用代碼:userManager *myManager=[userManager userManagerWithType:4];NSLog(@"使用者資訊:%@",myManager.toGetParams);

 

3:NSHTTPCookieStorage(擷取和刪除cookie)

取出cookieNSArray *cookiesArray = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];NSDictionary *cookieDict = [NSHTTPCookie requestHeaderFieldsWithCookies:cookiesArray];NSString *cookie = [cookieDict objectForKey:@"Cookie"];//設定http的header的cookie[urlRequest setValue:cookie forHTTPHeaderField:@"Cookie”];刪除cookieNSArray *cookiesArray = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];for (NSHTTPCookie *cookie in cookiesArray) {    [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];}

 

4:iOS關於在UITableView中,實現多個cell中不同的倒計時實現

//所有剩餘時間數組NSMutableArray *totalLastTime;在網路請求到的所有資料中,根據需要將其中要進行倒計時顯示的資料中的剩餘時間單獨儲存出來,如果這裡是所有的資料都需要倒計時,則只需要儲存時間即可,如果是有部分資料才需要倒計時,則可以儲存字典,兩個索引值對分別為其在UITableView的indexPath和剩餘時間:num預設從0開始NSDictionary *dic = @{@"indexPath":[NSStrin stringWithFormat:@"%i",num],@"lastTime": order.payLastTime}; [totalLastTime addObject:dic];開啟定時器方法:- (void)startTimer{    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:selfselector:@selector(refreshLessTime) userInfo:@"" repeats:YES]; 如果不添加下面這條語句,在UITableView拖動的時候,會阻塞定時器的調用    [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode]; }主要的定時器中的方法,在該方法中,遍曆totalLastTime,取出其中儲存的lasttime和indexpath,time用來顯示,在顯示完後自減,indexpath代表對應顯示的位置,在一次迴圈後,將新的time和沒有改變的indexpath從新替換totalLastTime 中對應位置的元素,以此保證每一秒執行時,顯示time都是最新的。- (void)refreshLessTime{    NSUInteger time;    for (int i = 0; i < totalLastTime.count; i++) {        time = [[[totalLastTime objectAtIndex:i] objectForKey:@"lastTime"]integerValue];        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:[[[totalLastTime objectAtIndex:i] objectForKey:@"indexPath"] integerValue]];        WLServiceOrderTableViewCell *cell = (WLServiceOrderTableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];        cell.remainingTimeLabel.text = [NSString stringWithFormat:@"剩餘支付時間:%@",[self lessSecondToDay:--time]];        NSDictionary *dic = @{@"indexPath": [NSStringstringWithFormat:@"%i",indexPath.section],@"lastTime": [NSStringstringWithFormat:@"%i",time]};        [totalLastTime replaceObjectAtIndex:i withObject:dic];    }}- (NSString *)lessSecondToDay:(NSUInteger)seconds{    NSUInteger day  = (NSUInteger)seconds/(24*3600);    NSUInteger hour = (NSUInteger)(seconds%(24*3600))/3600;    NSUInteger min  = (NSUInteger)(seconds%(3600))/60;    NSUInteger second = (NSUInteger)(seconds%60);        NSString *time = [NSString stringWithFormat:@"%lu日%lu小時%lu分鐘%lu秒",(unsigned long)day,(unsigned long)hour,(unsigned long)min,(unsigned long)second];    return time; }

 

5:如何運用Method Swizzling動態插入一些操作

假設工程中有很多ViewController,但有個操作每個頁面都有,以前都是每個頁面都去編寫相同的代碼,其實用Method Swizzling就可以解決這個問題,比如RDVTabBarController這個只在四個首頁才顯示出來,其它頁面都進行隱藏;

a:建立一個擴充類:UIViewController+Swizzle

.h檔案的內容:

#import <UIKit/UIKit.h>#import <objc/runtime.h>#import "RDVTabBarController.h"@interface UIViewController (Swizzle)@end

.m檔案的內容:

#import "UIViewController+Swizzle.h"@implementation UIViewController (Swizzle)+ (void)load{    SEL origSel = @selector(viewDidAppear:);    SEL swizSel = @selector(swiz_viewDidAppear:);    [UIViewController swizzleMethods:[self class] originalSelector:origSel swizzledSelector:swizSel];        SEL vcWillAppearSel=@selector(viewWillAppear:);    SEL swizWillAppearSel=@selector(swiz_viewWillAppear:);    [UIViewController swizzleMethods:[self class] originalSelector:vcWillAppearSel swizzledSelector:swizWillAppearSel];}+ (void)swizzleMethods:(Class)class originalSelector:(SEL)origSel swizzledSelector:(SEL)swizSel{    Method origMethod = class_getInstanceMethod(class, origSel);    Method swizMethod = class_getInstanceMethod(class, swizSel);        //class_addMethod will fail if original method already exists    BOOL didAddMethod = class_addMethod(class, origSel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));    if (didAddMethod) {        class_replaceMethod(class, swizSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));    } else {        //origMethod and swizMethod already exist        method_exchangeImplementations(origMethod, swizMethod);    }}- (void)swiz_viewDidAppear:(BOOL)animated{    //可以對控制器名稱做過濾 達到過濾哪些是不操作    NSString *curClassName=NSStringFromClass([self class]);    if (curClassName.length>0&&([curClassName isEqualToString:@"BDCustomerListViewController"]||[curClassName isEqualToString:@"BDOrdersViewController"]||[curClassName isEqualToString:@"BDDiscoverViewController"]||[curClassName isEqualToString:@"BDMineInfoViewController"])) {        [self.rdv_tabBarController setTabBarHidden:NO animated:YES];    }    //需要注入的代碼寫在此處    [self swiz_viewDidAppear:animated];}-(void)swiz_viewWillAppear:(BOOL)animated{    if ([[self.navigationController childViewControllers] count] > 1) {        [self.rdv_tabBarController setTabBarHidden:YES animated:YES];    }    [self swiz_viewWillAppear:animated];}@end
說明:+ (void)load 方法是一個類方法,當某個類的代碼被讀到記憶體後,runtime會給每個類發送 + (void)load 訊息。因此 + (void)load 方法是一個調用時機相當早的方法,而且不管父類還是子類,其 + (void)load 方法都會被調用到,很適合用來插入swizzling方法

b:調用在main.m引入這個擴充類

#import <UIKit/UIKit.h>#import "AppDelegate.h"#import "UIViewController+Swizzle.h"int main(int argc, char * argv[]) {    @autoreleasepool {        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));    } }

 

6:IOS關於UIImageView的展開問題

 

指定這4個寬度後 會形成黑色模組 直白點就是 這塊內容就是展開後中間那塊不斷填充的部分

UIImage* img=[UIImage imageNamed:@"2.png"];//原圖UIEdgeInsets edge=UIEdgeInsetsMake(0, 10, 0,10);     //UIImageResizingModeStretch:展開模式,通過展開UIEdgeInsets指定的矩形地區來填充圖片 上下左右都會     //UIImageResizingModeTile:平鋪模式,通過重複顯示UIEdgeInsets指定的矩形地區來填充圖img= [img resizableImageWithCapInsets:edge resizingMode:UIImageResizingModeStretch];self.imageView.image=img;項目中的運用:        self.myImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 184, 25)];        UIImage *curImage=[UIImage imageNamed:@"form_no_selected_icon"];        UIEdgeInsets edge=UIEdgeInsetsMake(0, 40, 0,25);        curImage= [curImage resizableImageWithCapInsets:edge resizingMode:UIImageResizingModeStretch];        self.myImageView.image=curImage;        [self.view addSubview:self.myImageView];

如果只是左右展開就用UIImageResizingModeStretch,關於edge的值左右自個根據實際的大小進行確定,把以這塊進行展開,特別要注意2X跟3X的圖片;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.