IOS筆記046-UIApplication/導航控制器

UIApplication

每個應用都有本身的UIApplication對象,並且是單例的
經過[UIApplication sharedApplication]能夠得到這個單例對象
一個iOS程序啓動後建立的第一個對象就是UIApplication對象ios

單例對象的獲取網絡

    UIApplication *app = [UIApplicationsharedApplication];app

隱藏狀態欄編輯器

    // 1、隱藏狀態欄,ios7之後默認是由控制器控制的,若是要用UIApplication控制請在info.plist中設置一個參數ide

    // UIViewController-based status bar system NO;函數

    [app setStatusBarHidden:YES];測試

屏幕快照 2015 06 10 10 46 48

顯示提醒框動畫

    // 2、引用提醒框,IOS8之後必須註冊通知才能使用ui

    app.applicationIconBadgeNumber = 14;this

    UIUserNotificationSettings *setting = [UIUserNotificationSetting ssettingsForTypes:UIUserNotificationTypeBadgecategories:nil];

    // 註冊通知

    [app registerUserNotificationSettings:setting ];

設置網絡加載狀態,在狀態欄顯示

    // 3、設置聯網狀態,在狀態欄顯示

    app.networkActivityIndicatorVisible = YES;

打開網頁或通話面板

    // 4、打開網頁,打電話,發郵件等

    // URL : 協議頭+域名(路徑)  http/ftp/tel

    // 系統經過協議頭來判斷具體要打開何種軟件

    UIApplication *app = [UIApplicationsharedApplication];

    NSURL *url = [NSURLURLWithString:@"http://www.qq.com"]; // http 會打開Safari

    [app openURL: url];

info.plist文件

能夠經過mainBundle 獲取info.plist的一些屬性。下面是一些已經定義好的。

@property (readonly, copy) NSString *bundleIdentifier;

@property (readonly, copy) NSDictionary *infoDictionary;

@property (readonly, copy) NSDictionary *localizedInfoDictionary;

 

屏幕快照 2015 06 10 12 33 48

常見屬性(紅色部分是用文本編輯器打開時看到的key)

    Localiztion native development region(CFBundleDevelopmentRegion)-本地化相關

    Bundle display name(CFBundleDisplayName)-程序安裝後顯示的名稱,限制在1012個字符,若是超出,將被顯示縮寫名稱

    Icon file(CFBundleIconFile)-app圖標名稱,通常爲Icon.png

    Bundle version(CFBundleShortVersionString)-應用程序的版本號,每次往App Store上發佈一個新版本時,須要增長這個版本號

    Main storyboard file base name(NSMainStoryboardFile)-storyboard文件的名稱

    Bundle identifier(CFBundleIdentifier)-項目的惟一標識,部署到真機時用到

pch文件

    項目的Supporting files文件夾下面有個工程名-Prefix.pch」文件,也是一個頭文件

    pch頭文件的內容能被項目中的其餘全部源文件共享和訪問

    通常在pch文件中定義一些全局的宏

    pch文件中添加下列預處理指令,而後在項目中使用Log(…)來輸出日誌信息,就能夠在發佈應用的時候,一次性將NSLog語句移除(在調試模式下,纔有定義DEBUG

        #ifdef DEBUG

        #define Log(...) NSLog(__VA_ARGS__) // … 表示宏可變參數,__VA_ARGS__ 表示函數可變參數

        #else

        #define Log(...)

        #endif

 

AppDelegate

        屏幕快照 2015 06 10 13 02 26

每個工程都會自動生成一個代理文件,這個代理文件的一些方法以下,還有調用順序。

    // 1、啓動完成後調用

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        // Override point for customization after application launch.

        return YES;

    }

    // 3、應用程序失去焦點後調用,再進入後臺

    - (void)applicationWillResignActive:(UIApplication *)application {

        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    }

    // 4、應用進入後臺調用,接電話,鎖屏,按下home建等操做

    - (void)applicationDidEnterBackground:(UIApplication *)application {

        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    }

    // 5、應用進入前臺後調用,先進入前臺再得到焦點 2

    - (void)applicationWillEnterForeground:(UIApplication *)application {

        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

    }

    // 2、應用得到焦點,可與用戶交互

    - (void)applicationDidBecomeActive:(UIApplication *)application {

        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    }

    // 應用終止運行會調用,實際上得調用也沒法測試

    - (void)applicationWillTerminate:(UIApplication *)application {

        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

        // Saves changes in the application's managed object context before the application terminates.

        [self saveContext];

    }

由前臺進入後天過程:先失去焦點,再進入後臺。

有後臺進入前臺過程:先進入前臺,再得到焦點。

main.m文件

每一個程序都要有個main函數,ios也同樣。默認位置在Supporting Files文件下。

屏幕快照 2015 06 10 13 10 50

    int main(int argc, char * argv[]) {

        @autoreleasepool {

            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegateclass]));

        }

    }

在Xcode中 main函數只調用了一個方法 UIApplicationMain 。

UIApplicationMain 方法作了一些初始化的操做:

        一、第三個參數:建立一個 UIApplication 對象,默認nil就返回 UIAplication 對象

        二、第四個參數:建立一個 UIApplicationDelegate 代理對象,而後設置UIApplication.delegate = UIApplicationDelegate

        三、設置一個主循環,可保持程序一直出去運行狀態,開始接受事件

        四、加載info.plist ,而後加載main.storyboard 

 

main.storyboard 的加載過程

       一、初始化一個窗口Window

       二、加載main.storyboard 而且初始化控制器

       三、設置view的rootViewController並顯示到屏幕

 

真正的加載過程

在代理方法中使用,程序啓動完成後建立窗口並顯示。

大概過程以下:

        // 1、建立窗口

        self.window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];

        self.window.backgroundColor = [UIColorredColor];

        // 2、建立一個控制器,並指定window的父控制器

        UIViewController *vc = [[UIViewController alloc] init];

        self.window.rootViewController = vc;

        // 3、顯示窗口

        [self.window makeKeyAndVisible];

關於第二步建立一個控制器,還有幾種方法

       方法1:使用代碼

        // 方法1 代碼

        UIViewController *vc = [[UIViewControlleralloc] init];

        self.window.rootViewController = vc;

       方法2:使用storyboard

        // 方法2 storyboard建立

        // 參數bundlenil時指 mainBundle

        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        // 默認加載箭頭指向的那個控制器

        UIViewController *vc1 = [story instantiateInitialViewController];

        // 使用標識加載控制器,可在storyboard 裏進行設置

        UIViewController *vc2 = [story instantiateViewControllerWithIdentifier:@"green"];

        self.window.rootViewController = vc1;

        屏幕快照 2015 06 10 18 47 48

       方法3:使用xib

        // 方法3 xib建立

        /* 注意事項

        經過xib建立控制器的view

        必定要描述xib的文件擁有者是控制器,也就是說這個xib是用來描述控制器

        */

        UIViewController *vc3 = [[ViewController alloc] initWithNibName:@"abc" bundle:nil];

        //UIViewController *vc3 = [[ViewController alloc] init];

        /**

         加載跟類名相同的xib

         若是描述控制器Viewxib跟控制器的類名相同,就會去加載

         只有控制器的init方法底層會調用initWithNibName:bundle:

         只要經過initWithNibName:bundle:初始化控制器,而且nibNamenil,就會執行如下幾步。

         SLQViewController類型

            1.尋找有沒有跟控制器類名同名可是不帶Controllerxib,若是有就會去加載(XXX.xib

            2.尋找有沒有跟控制器類名同名的xib,若是有就會去加載(SLQViewController.xib)

            3.若是都沒有找到,建立空的view

        */

        UIViewController *vc4 = [[ViewController alloc] initWithNibName:nil bundle:nil];

        self.window.rootViewController = vc3;

經過xib建立系統會作不少判斷,必定要注意。

默認建立的空view是近乎透明的

        // 2、建立一個控制器

        // 建立UIViewController控制器,控制器的view並無建立

        // 控制器的view懶加載:第一次使用的時候纔會去加載,並非建立UIViewController控制器的時候去加載

        UIViewController *vc = [[UIViewControlleralloc] init];

        // vc.view.backgroundColor = [UIColor clearColor]; //相似設置clearColor屬性

        // vc.view.alpha = 1;

        // 默認建立的空view是近乎透明的,不能穿透。

        self.window.rootViewController = vc;

自定義view須要注意的地方

在控制器中的loadView方法中自定義本身的view

    // 默認loadView能夠加載storyboardxib描述的控制器的view

    // 若是想自定義view,能夠在這裏進行操做

    - (void)loadView

    {

        // 若是控制器是窗口的根控制器就能夠不用設置尺寸

        self.view = [[UIView alloc] initWithFrame:CGRectZero];

        self.view.backgroundColor = [UIColoryellowColor];

    }

    // viewgetter方法相似下面

    //- (UIView *)view

    //{

    //    if (_view == nil) {

    //        [self loadView];

              // 。。。。。

    //        [self viewDidLoad];

    //    }

    //    return _view;

    //}

    // viewDidLoad中打印控制器的尺寸不許確,一般在viewDidAppear

    // 控制器的view加載完成的時候調用

    - (void)viewDidAppear:(BOOL)animated

    {

        [super viewDidAppear:animated];

        NSLog(@"%@",NSStringFromCGRect(self.view.bounds));

    }

UINavigation - 導航控制器

建立過程

    // 1、建立窗口

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColorredColor];

    

    // 2、建立控制器

    UIViewController *vc = [[FirstViewControlleralloc] init];

    // UINavigationController 初始化

    // 導航控制器也須要一個根控制器

    // 默認導航控制器把根控制器的view添加到導航控制器的view

    UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:vc];

    self.window.rootViewController = nav;

    

    // 3、顯示窗口

    [self.window makeKeyAndVisible];

 導航控制的使用,主要是如下幾個方法。

 UINavigationController以棧的形式保存子控制器

        @property(nonatomic,copy) NSArray *viewControllers;
        @property(nonatomic,readonly) NSArray *childViewControllers;

使用push方法能將某個控制器壓入棧
        - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
使用pop方法能夠移除控制器
將棧頂的控制器移除
        - (UIViewController *)popViewControllerAnimated:(BOOL)animated;
回到指定的子控制器
        - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
回到根控制器(棧底控制器)
        - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

添加跳轉界面

屏幕快照 2015 06 10 22 30 22

首先控制器的頭文件

而後建立一個控制器

最後入棧。

- (IBAction)jumpToSecond:(id)sender

{

    // 建立控制器

    UIViewController *vc = [[SecondViewController alloc] init];

    // 入棧

    [self.navigationController pushViewController:vc animated:YES];

    // 修改頂部標題

    self.navigationItem.title = @"上一頁";

 

}

屏幕快照 2015 06 10 22 49 46 

回到上一個View中

- (IBAction)jumpToFirst:(id)sender

{

    // 彈出最底部的view

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

}

- (IBAction)jumpToPre:(id)sender

{

    // 彈出最頂部的view

    [self.navigationControllerpopViewControllerAnimated:YES];

}

導航欄的標題相關

顯示標題和左上角的返回按鈕以及右上角的其餘按鈕

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        // 設置本控制的標題

        self.navigationItem.title = @"第二個控制器";

        // 顯示在下一個跳轉界面的左上角的返回按鈕

        self.navigationItem.backBarButtonItem = [[UIBarButtonItemalloc] initWithTitle:@"返回"style:UIBarButtonItemStyleDonetarget:nilaction:nil];

        // 顯示在右邊的自定義按鈕

        UIBarButtonItem *first = [[UIBarButtonItemalloc] initWithTitle:@"haha"style:UIBarButtonItemStyleDonetarget:nilaction:nil];

        UIBarButtonItem *second = [[UIBarButtonItemalloc] initWithImage:[UIImageimageNamed:@"doc_praised_icon_night"] style:UIBarButtonItemStylePlaintarget:nilaction:nil];

        self.navigationItem.rightBarButtonItems@[first,second];

    }

       屏幕快照 2015 06 11 11 48 45

顯示自定義的按鈕

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        // Do any additional setup after loading the view from its nib.

        self.view.backgroundColor = [UIColordarkGrayColor];

        self.navigationItem.title = @"第三個控制器";

        // 頂部左邊顯示按鈕

        self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc] initWithImage:[UIImageimageNamed:@"doc_praised_icon_night"] style:UIBarButtonItemStyleDonetarget:nilaction:nil];

        // 顯示自定義view:按鈕

        UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(0, 0, 40, 40);

        [btn setImage:[UIImageimageNamed:@"doc_praised_icon_night"] forState:UIControlStateNormal];

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc] initWithCustomView:btn];;

    }

        屏幕快照 2015 06 11 12 35 32

能夠任意顯示本身想用的控件,如添加如下兩行便可顯示一個開關控件。

    // 頂部左邊顯示按鈕

    UISwitch *sw = [[UISwitchalloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc] initWithCustomView:sw];

       屏幕快照 2015 06 11 12 45 02

 storyboard實現控制器之間的跳轉

自動跳轉型

一、先拖拽一個NavigationController 而後設置rooterViewController

屏幕快照 2015 06 11 13 22 10

二、而後再其餘的控制器中連線就能夠創建跳轉關係。這個gif動畫有點大啊,慢慢加載吧.

手動跳轉型

一、 先拖拽一個NavigationController 而後設置rooterViewController

二、在第一個控制器上右鍵單擊view出新manual而後想要跳轉的控制器拖線,選擇push

屏幕快照 2015 06 11 16 30 53

 三、設置連線的標識爲「redToBlue」

 屏幕快照 2015 06 11 16 34 39

 四、在第一個控制器中經過代碼控制跳轉,響應按鈕按下

    - (IBAction)jumpToBlue:(id)sender

    {

        // 使用代碼進行跳轉,經過標識判斷是哪一條線

        [selfperformSegueWithIdentifier:@"redToBlue"sender:nil];

    }

相關文章
相關標籤/搜索