با عرض سلام خدمت کاربران سایت پی وی لرن ، قسمت دوم جلسه چهارم از آموزش دوره iOS را آغاز می کنیم.این جلسه می خواهیم ادامه مطالب ساخت برنامه آیفون در iOS را ارائه دهیم،این برنامه یک صفحه خالی در Xcode است و بسیار ساده است برای اینکه شما بهتر یاد بگیرید و در نهایت در شبیه ساز این برنامه را تست کنیم.
قسمت اول کد برنامه iOS
شما پنج فایل مختلف را که در برنامه شما تولید شده است پیدا خواهید کرد. آنها به شرح زیر می باشند:
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m
ViewController.xib
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // Header File that provides all UI related items. #import <UIKit/UIKit.h> // Forward declaration (Used when class will be defined /imported in future) @class ViewController; // Interface for Appdelegate @interface AppDelegate : UIResponder <UIApplicationDelegate> // Property window @property (strong, nonatomic) UIWindow *window; // Property Viewcontroller @property (strong, nonatomic) ViewController *viewController; //this marks end of interface @end |
AppDelegate از UIResponder که رویدادهای iOS را مدیریت می کند به ارث می برد.
روشهای نمایندگی UIApplicationDelegate را اجرا می کند که رویدادهای کاربردی کلیدی مانند راه اندازی کامل، خاتمه دادن و غیره را فراهم می کند.
UIWindow شی برای مدیریت و هماهنگ کردن نمایش های مختلف بر روی صفحه نمایش دستگاه iOS.
این همانند دیدگاه پایه ای است که تمام نظرات دیگر بارگذاری می شود. به طور کلی تنها یک پنجره برای یک برنامه وجود دارد.
UIViewController برای کنترل جریان صفحه نمایش.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | // Imports the class Appdelegate's interface import "AppDelegate.h" // Imports the viewcontroller to be loaded #import "ViewController.h" // Class definition starts here @implementation AppDelegate // Method to intimate us that the application launched successfully - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(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.*/ } - (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.*/ } - (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:. */ } - (void)applicationWillTerminate:(UIApplication *)application { /* Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. */ } @end |
نمایندگان UIA در اینجا تعریف شده اند. تمام روش های تعریف شده در بالا نمایندگان برنامه های کاربردی UI هستند و شامل هیچ روش تعریف شده توسط کاربر نیستند.
شیء UIWindow اختصاص داده شده برای نگهداری برنامه اختصاص داده شده است.
UIViewController به عنوان کنترل اولیه نمایش پنجره اختصاص داده شده است.
برای ایجاد پنجره قابل رویت، روش MakeKeyAndVisible نامیده می شود.
1 2 3 4 5 6 | #import <UIKit/UIKit.h> // Interface for class ViewController @interface ViewController : UIViewController @end |
کلاس ViewController کنترلر UIViewController را به ارمغان می آورد، که مدل مد نظر view را برای برنامه های iOS را فراهم می کند.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #import "ViewController.h" // Category, an extension of ViewController class @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end |
دو روش انجام شده در اینجا در کلاس پایه UIViewController تعریف شده است.
تنظیم اولیه در viewDidLoad که پس از نمایش بار خوانده می شود.
روش didReceiveMemoryWarning در صورت هشدار حافظه فراخوانی می شود.
جلسه پنجم از آموزش های دوره iOS با ساخت برنامه برای آیفون به پایان رسید با ما همراه باشید تا در جلسه بعد آموزش اقدامات و رسانه ها در iOS را شروع کنیم.