NaviConのサンプルコード
特に何ができるわけではないけれどNavigationCotrollerの練習になりますよ〜
1ページ目
2ページ目
3ページ目
※1つ目を押すと一つ前に戻る
テキストフィールドは特になにか動作する訳ではない(文字を入れれるだけ)
画像はボタンとして設定しているが特に動作しない
ゴミ箱ボタンは1つ前に戻る
ブックマークは一番最初のページに戻る
まずは下準備をしましょう
(SubClassはUIViewControllerで作成してください)
ViewController.hに記述
#import <UIKit/UIKit.h>
#import "ViewController1.h"
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
UINavigationBar *_navBar;
UINavigationItem *_navItem1;
}
@end
----------------------------------------------------------------------------------------
ViewController.mに記述
#import "ViewController.h"
@interfaceViewController ()
enum SECTION { SET ,SECTION_MAX };
@property(retain) NSMutableArray *settingArray,*subArray;
@property(retain) NSMutableArray *hiraganaArray;
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a ni
//このビューコントローラーのタイトル
self.title = @"設定";
//表示データ
self.settingArray = [NSMutableArrayarrayWithObjects:@"あいつをロックオン",@"あいつのWi-Fi",@"あいつの動きを通知",@"あいつの位置情報サービス",@"あいつのキャリア",nil];
self.subArray = [NSMutableArrayarrayWithObjects:@"",@"AP5A",@"",@"オン",@"NTTCodomo",nil];
self.view.backgroundColor = [UIColorlightGrayColor];
//テーブルビューの作成
UITableView *tv = [[[UITableViewalloc] initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped] autorelease];//スタイルを変えれる(枠のあるテーブルビューを作れる)
tv.tag = 100;
//デリゲートは自分
tv.delegate = self;
//データソーズも自分
tv.dataSource = self;
//ビューに配置する
[self.view addSubview:tv];
}
#pragma mark -UITableVIewDataSource
//セクションごとのデータ件数を教えてあげるメソッド
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
returnself.settingArray.count;
}
//各セルの作成
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"要求されたデータはsection = %d row = %d" , indexPath.section, indexPath.row);
//Cellという識別子で昔作ったUITableViewCellがあるかどうか取得してみる
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
//cell変数の戻りがnilが変えるときは
if( cell == nil ){
//前に作ったものはないので、インスタンスを生成する。その際再利用を考えCellという識別子をつけておく
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleValue1reuseIdentifier:@"Cell"] autorelease];
}else{
//再利用すると、allocのコストがかからない
NSLog(@"同じ形式のCellなので再利用しています");
}
//画面から隠れた部分は再度作るか再利用されます
if( indexPath.section == SET ){
cell.textLabel.text = [self.settingArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [self.subArray objectAtIndex:indexPath.row];//NSArrayを詳細文字にする
cell.imageView.image = [UIImage imageNamed:@"EyeOpen.png"];//(画像をのせる)
//[cell.contentView addSubview:[UIButton buttonWithType:UIButtonTypeRoundedRect]];//ボタンを表示する
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//アクセサリーのタイプを指定
if(indexPath.row == 0){
cell.imageView.image = [UIImage imageNamed:@"sample.png"];//個別に画像を変える
cell.accessoryView =[[ UISwitch alloc] init];//スイッチをつくって貼付ける
}
} return cell;
}
//セクション(テーブル)にタイトルをつける(↓はタイトルをつけるためのメソッド名)
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"設定";
}
#pragma mark -UITableViewDelegate
//セルの選択されるイベントが発生
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//ViewController1クラスを作って
ViewController1 *vc1 = [[ViewController1alloc] init];
//ナビゲーションコントローラーへ追加する(上に重なる)
[self.navigationControllerpushViewController:vc1 animated:YES];
//選ばれると青くなるので、解除 animatedをNOにするとパッと消える YESだとほわーっと消える
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
---------------------------------------------------------------------------------------------
ViewController1.hに記述
#import <UIKit/UIKit.h>
#import "ViewController2.h"
@interface ViewController1 : UIViewController
@end
----------------------------------------------------------------------------------
ViewController1.mに記述
#import "ViewController1.h"
@interfaceViewController1 ()
@end
@implementation ViewController1
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorwhiteColor];
UILabel *label = [[UILabel alloc] init];
label.text = @"1つ目のビューコントローラーです";
[label sizeToFit];
[self.view addSubview:label];
self.title = @"1つ目";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc] initWithTitle:@"次へ"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(next:)];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)next:(UIBarButtonItem*)barButton
{
//ViewContoller2クラスを作って
ViewController2 *vc2 = [[ViewController2alloc] init];
//ナビゲーションコントローラーへ追加する(上に重なる)
[self.navigationControllerpushViewController:vc2 animated:YES];
}
@end
-----------------------------------------------------------------------------------
ViewController2.hに記述
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController
@end
-----------------------------------------------------------------------------------
ViewController2.mに記述
#import "ViewController2.h"
@interfaceViewController2 ()
@end
@implementation ViewController2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorwhiteColor];
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByCharWrapping;
label.text = @"2つ目のビューコントローラーです";
[label sizeToFit];
[self.view addSubview:label];
self.title = @"2つ目";
/*------BarButtonItemをナビゲーションバー右側に複数登録する--------*/
UIBarButtonItem *barButton1 = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarkstarget:selfaction:@selector(tapBook:)];
UIBarButtonItem *barButton2 = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrashtarget:selfaction:@selector(tapTrash:)];
UIImageView *iv = [[[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"EyeOpen.png"]] autorelease];
UIBarButtonItem *barButton3 = [[UIBarButtonItemalloc] initWithCustomView:iv];
//rigthBarButtonItem[s]を使うとios5からは複数のボタンが配置できます
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:barButton1,barButton2,barButton3,nil];
//テキストフィールドの作成
UITextField *tf = [[UITextFieldalloc] init];
tf.frame = CGRectMake(0,0,100,30);
tf.borderStyle = UITextBorderStyleRoundedRect;
tf.text = self.title;
//titleViewはタイトル部分のViewで入れ替え可能
self.navigationItem.titleView = tf;
//ナビゲーションバーが大きくなるpromptというプロパティもあります
self.navigationItem.prompt = @"ゴミ箱タップで1つ、本で先頭に戻ります";
//ナビゲーションコントローラーには実はツールバーもついてきていて、NOと設定するのみで表示可能
self.navigationController.toolbarHidden = NO;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//ゴミ箱のタップ
-(void)tapTrash:(UIBarButtonItem*)button
{
//1つ戻る
[self.navigationControllerpopViewControllerAnimated:YES];
}
//本のタップ
- (void)tapBook:(UIBarButtonItem*)button
{
//先頭に戻る
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
@end