Ios-first-iphone-application

提供:Dev Guides
移動先:案内検索

iOS-最初のiPhoneアプリケーション

最初のアプリの作成

次に、iOSシミュレーターで実行するシンプルなシングルビューアプリケーション(空のアプリ)を作成します。

手順は次のとおりです。

ステップ1 *-Xcodeを開き、 *Create a new Xcode project を選択します。

Xcode Welcomeページ

  • ステップ2 *-*シングルビューアプリケーション*を選択します。

プロジェクトの作成

  • ステップ3 *-製品名、つまり、アプリケーションの名前、組織名、次に会社識別子を入力します。

新規プロジェクト作成オプション

ステップ4 *-範囲外になったときに割り当てられたリソースを自動的に解放するために、 *Use Reference Reference Counting が選択されていることを確認します。 Nextをクリックしてください。

  • ステップ5 *-プロジェクトのディレクトリを選択し、作成を選択します。

プロジェクト選択フォルダーの作成

  • ステップ6 *-次のような画面が表示されます-

Xcodeプロジェクトページ

上の画面で、サポートされている向き、ビルド、リリースの設定を選択できます。 サポートするデバイスバージョンであるフィールド展開ターゲットがあり、4.3を選択できます。これは現在許可されている最小展開ターゲットです。 今のところ、これらは必須ではありません。アプリケーションの実行に焦点を当てましょう。

  • ステップ7 *-次に、Runボタンの近くのドロップダウンでiPhoneシミュレーターを選択し、runを選択します。

image、width = 200、height = 100

  • ステップ8 *-それだけです。最初のアプリケーションが正常に実行されました。 次のように出力が得られます-

image、width = 200、height = 360

次に、インターフェイスビルダーで開始するために、背景色を変更しましょう。 ViewController.xibを選択します。 右側で背景オプションを選択し、色を変更して実行します。

インターフェイスビルダー

上記のプロジェクトでは、デフォルトで、デプロイメントターゲットがiOS 6.0に設定され、自動レイアウトが有効になります。 iOS 4.3以降のデバイスでアプリケーションを実行するために、このアプリケーションの作成の開始時にデプロイメントターゲットをすでに変更していますが、自動レイアウトを無効にしませんでした。

自動レイアウトを無効にするには、各nibのファイルインスペクター、つまりxibファイルの自動レイアウトチェックボックスをオフにする必要があります。 XcodeプロジェクトIDEのさまざまなセクションを次の図に示します(提供:Apple Xcode 4ユーザードキュメント)。

Xcode 4ワークスペース

上記のように、ファイルインスペクタはインスペクタセレクタバーにあり、自動レイアウトはオフにできます。 自動レイアウトは、iOS 6デバイスのみをターゲットにする場合に使用できます。 また、展開ターゲットをiOS 6に上げると、通帳などの多くの新機能を使用できるようになります。 とりあえず、iOS 4.3を展開ターゲットとして使いましょう。

最初のiOSアプリケーションのコード

アプリケーション用に生成された5つの異なるファイルがあります。 それらは次のようにリストされています-

  • AppDelegate.h
  • AppDelegate.m
  • ViewController.h
  • ViewController.m *ViewController.xib

AppDelegate.h

//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は、iOSイベントを処理するUIResponderを継承します。
  • UIApplicationDelegateのデリゲートメソッドを実装します。このメソッドは、起動完了、終了間近などの主要なアプリケーションイベントを提供します。
  • iOSデバイス画面のさまざまなビューを管理および調整するUIWindowオブジェクト。 他のすべてのビューがロードされるベースビューのようなものです。 一般に、アプリケーションに対して1つのウィンドウのみがあります。 *画面フローを処理するUIViewController。

AppDelegate.m

//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

コードの重要な項目-

  • UIApplicationデリゲートはここで定義されます。 上記で定義されたすべてのメソッドはUIアプリケーションのデリゲートであり、ユーザー定義のメソッドは含まれていません。
  • UIWindowオブジェクトは、割り当てられたアプリケーションを保持するために割り当てられます。
  • UIViewControllerは、ウィンドウの初期View Controllerとして割り当てられます。
  • ウィンドウを表示するには、makeKeyAndVisibleメソッドが呼び出されます。

ViewController.h

#import <UIKit/UIKit.h>

//Interface for class ViewController
@interface ViewController : UIViewController

@end

コードの重要な項目-

  • ViewControllerクラスは、iOSアプリケーションの基本的なビュー管理モデルを提供するUIViewControllerを継承します。

ViewController.m

#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

コードの重要な項目-

  • ここで実装される2つのメソッドは、基本クラスUIViewControllerで定義されています。
  • ビューのロード後に呼び出されるviewDidLoadで初期セットアップを実行します。
  • didReceiveMemoryWarningメソッドは、メモリ警告の場合に呼び出されます。