Objective-C,viewが動作しない

このQ&Aのポイント
  • Objective-Cのカメラアプリでviewが表示されない問題について調査しています。
  • AppDelegate.m内のapplication didFinishLaunchingメソッドで生成したwindowとviewは表示されますが、viewControllerで書いたUIImagePickerViewControllerのviewが表示されません。
  • viewDidLoadメソッドやloadViewメソッドも動作していないようです。原因を特定できずに困っています。
回答を見る
  • ベストアンサー

Objective-C,viewが動作しない

IBを使わないカメラアプリを作っています。 AppDelegate.m内のapplication didFinishLaunchingメソッド内で生成したwindowとviewはコンパイルして表示確認できたのですが、viewControllerで書いたUIImagePickerViewControllerのviewが出てきません。 そもそもviewController.mのloadViewメソッドやviewDidLoadメソッドが動いていません。(メソッド内にNSLogを書いてコンパイルしてもログに出ない) viewController.h #import <UIKit/UIKit.h> @interface CameraViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate> { @private UIImagePickerController* _imagePicker; } @property(nonatomic,retain) UIImagePickerController *imagePicker; @end viewController.m #import "CameraViewController.h" @implementation CameraViewController @synthesize imagePicker=_imagePicker; - (void)dealloc { [super dealloc]; } #pragma mark - View lifecycle - (void)loadView { [super loadView]; //UIImagePickerControllerの作成 if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ return; } UIImagePickerController* imagePicker; imagePicker = [[UIImagePickerController alloc] init]; [imagePicker autorelease]; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.allowsEditing=NO; imagePicker.delegate = self; //imagePickerのviewを最上部に [self presentModalViewController:imagePicker animated:YES]; //シャッターを切る [self.imagePicker takePicture]; } @end viewControllerが機能しない原因としてそういった可能性が考えられるのでしょうか? あれこれ調べてみましたが煮詰まっています。よろしくお願いします

質問者が選んだベストアンサー

  • ベストアンサー
  • harawo
  • ベストアンサー率58% (3742/6450)
回答No.2

いや、逆にインスタンス変数と、プロパティの宣言のほうがいらないんでしょう。

neglectiii
質問者

お礼

ご指摘の内容を参考に試行錯誤してみたらできました。 ともかく前提としてインスタンス変数の宣言とインスタンス生成はどちらか一方で良いということですね。ありがとうございました!

その他の回答 (1)

  • harawo
  • ベストアンサー率58% (3742/6450)
回答No.1

- (void)loadView { [super loadView]; //UIImagePickerControllerの作成 if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ return; } UIImagePickerController* imagePicker; imagePicker = [[UIImagePickerController alloc] init]; [imagePicker autorelease]; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.allowsEditing=NO; imagePicker.delegate = self; //imagePickerのviewを最上部に [self presentModalViewController:imagePicker animated:YES]; //シャッターを切る [self.imagePicker takePicture]; } この中の9行目、 UIImagePickerController* imagePicker; を削除してみてください。 この行があると、インスタンス変数で、imagePickerを宣言している意味がありません。

関連するQ&A

  • Obj-Cで生成元のViewへ文字列を表示させたい

    iOS SDK(Objective-C)を勉強中なのですが、あるView(TestViewController.view)から別のView(ButtonViewController.view)を表示し、表示したViewにあるボタンを押した時に、生成元のView(TestViewController.view)のラベル(test_label)に文字列を表示したいのですが、色々やってみたものの上手く行かず、どの用に実装するのが正しいのか行き詰まってしまいました。どなたか手助けよろしくお願いします。 //*** TestViewController.h #import <UIKit/UIKit.h> #import "ButtonViewController.h" @interface TestViewController : UIViewController { IBOutlet UILabel *test_label; ButtonViewController *buttonViewController; } - (IBAction) pushButton:(id)sender; @end //*** TestViewController.m #import "TestViewController.h" @implementation TestViewController - (void)viewDidLoad { [super viewDidLoad]; test_label.text = @"Push Button"; } //ButtonViewを表示 - (IBAction) pushButton:(id)sender { buttonViewController = [[ButtonViewController alloc] initWithNibName:@"ButtonView" bundle:nil]; [self.view addSubview:buttonViewController.view]; } - (void)dealloc { [buttonViewController release]; [super dealloc]; } @end //*** ButtonViewController.h #import <UIKit/UIKit.h> @interface ButtonViewController : UIViewController { } - (IBAction) pushButtonSub:(id)sender; @end //*** ButtonViewController.m #import "ButtonViewController.h" @implementation ButtonViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction) pushButtonSub:(id)sender { // ??? ← このボタンを押したときにtest_labelへ表示させたい } - (void)dealloc { [super dealloc]; } @end

  • objective-c 初心者です

    はじめまして。iphone アプリ開発を始めようと思い、まずは簡単なアプリからと人のまねをして時計アプリを書いております。http://www.slideshare.net/takuya0429/121216の43枚目のスライドを丸写しにしました。しかし、iPhoneシミュレーター(iphone 6.1)でシミュレーションしても真っ黒な画面が移されるだけで思うように機能しません。 なぜ機能しないかを考えられる範囲でご教授願いたい次第です。 環境はmacbook air 13inch にてXcode 4.6.3を使用しております。 以下に作成したコードを記載いたします。 // // ViewController.h // watch // // Created by motoyama kaoru on 2013/08/15. // Copyright (c) 2013年 motoyama kaoru. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *timeLabel; @end // // ViewController.m // watch // // Created by motoyama kaoru on 2013/08/15. // Copyright (c) 2013年 motoyama kaoru. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES]; } -(void)update{ NSDate *now = [NSDate date]; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"HH:mm:ss"]; self.timeLabel.text = [df stringFromDate:now]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

  • Xcode4のエラー(IPhoneアプリ)

    はじめまして。 IPhoneのアプリ作成に挑戦しています。 Xcode4のバージョンは 4.5.1です。 【はじめてのXcode4プログラミング】という参考書でIPhoneのカメラアプリを作る章があり、写真ライブラリから選択した画像を表示できるとのことです。 しかし、XcodeでRun(実行)すると、ViewController.mの最後2行【UIImage *originalImage =[info objectForkey:UIImagePickerControllerOriginalImage];】が赤くなり、【’dismissModalViewControllerAnimated’is deprecated: first deprecated in iOS 6.0】というエラー表示がでます。 全体のコードは下記の通りです。 ネットで調べてみましたが、解決方法がわかりません。 先に進めず困っています。 どうか解決方法を教えてください。 よろしくお願い致します。 ●ViewController.h// // ViewController.h // CameraApp // #import @interface ViewController : UIViewController - (IBAction)pressCameraButton:(id)sender; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end ●ViewController.m // // ViewController.m // CameraApp // #import “ViewController.h” @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. } - (IBAction)pressCameraButton:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentModalViewController:picker animated:YES]; } - (void)imagePickerController:(UIImagePickerController*)Picker didFinishPickingMediaWithInfo:(NSDictionary*)info { [self dismissModalViewControllerAnimated:YES]; UIImage *originalImage = [info objectForkey:UIImagePickerControllerOriginalImage]; self.imageView.image = originalImage; } @end

  • Xcodeでの画面遷移について

    Xcode5.0でアプリを作成しています。 アプリがバックグラウンドから再びアクティブになるときに画面遷移の処理をするために 以下の方法にてAppDelegateのapplicationDidBecomeActiveからView Controllerに処理を渡しています。 AppDelegate.m - (void)applicationDidBecomeActive:(UIApplication *)application { [[NSNotificationCenter defaultCenter] postNotificationName:@"applicationDidBecomeActive" object:nil]; } ViewController.m { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive) name:@"applicationDidBecomeActive" object:nil]; } - (void)applicationDidBecomeActive { ViewController *View = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondView"]; [self presentModalViewController:View animated:YES]; } ViewController.mの- (void)applicationDidBecomeActiveで画面遷移の処理をしたいのですが、ViewDidAppearでは出来る画面遷移がapplicationDidBecomeActiveでは実行されない状態です。 ググって調べたところ、読み込みのタイミングが問題らしいことはなんとなく解ったのですが、 どうにかして、- (void)applicationDidBecomeActiveと同じタイミング(アプリがアクティブになったとき)で画面遷移を行う方法はありますでしょうか?

  • [objective-c]他クラスのメソッドを呼ぶ

    objecive-cを使ってアプリを作っているのですが、 iphoneアプリの開発初心者です。 action.mファイルで指定しているボタンのアクションのところで、 kekka.mのファイルに書いているメソッドを実行させたいのですが、 他クラスのメソッドの実行方法が分かりません。 [インスタンス メソッド名]; で実行できるということは何となく分かったのですが、 どれがインスタンスに当たるのかが不明で、質問しました。 action.mに書いたメソッド ------------------- //次へのボタンが押されたときの処理 - (IBAction)kaisetsu:(id)sender { //kekka.mの- (void)showNextメソッドを呼び出したい。 [★ここの書き方が分かりません★]; kekka *viewController = [[kekka alloc] initWithNibName:@"kekka" bundle:nil]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent; [navigationController setNavigationBarHidden:YES animated:YES]; //モーダルビューとして、次のビューを表示する [self presentModalViewController:navigationController animated:YES]; } ------------------- kekka.mに書いているメソッド ------------------------ // 結果を表示する - (void)showNext { // 結果情報を取得する QuizItem *item = [self.quiz nextQuiz]; self.questionTextView.text = item.question; //読み込んで追加========================= NSUserDefaults *_userDefaults = [NSUserDefaults standardUserDefaults]; NSString *kaisetsutext; kaisetsutext = [_userDefaults stringForKey:@"kaisetsutext"]; //読み込み kaisetsutext = item.kaisetsu; [_userDefaults setObject:kaisetsutext forKey:@"kaisetsutext"]; [_userDefaults synchronize]; //読み込んで追加========================= } お手数ですが、ご教授いただけましたら幸いです。

  • iPhoneアプリ作成時のXcodeのエラー

    はじめまして。 iPhoneのアプリ作成に挑戦しています。 Xcode4のバージョンは 4.5.1です。 【はじめてのXcode4プログラミング】という参考書でIPhoneのカメラアプリを作る章があり、写真ライブラリから選択した画像を表示できるとのことです。 しかし、XcodeでRun(実行)すると、ViewController.mの最後から4行目【UIImage *originalImage = [info objectForkey:UIImagePickerControllerOriginalImage];】が赤くなり、【No visible @interface for 'NSDictionary' declares the selector 'objectForkey'】というエラー表示がでます。 全体のコードは下記の通りです。 ネットで調べてみましたが、解決方法がわかりません。 先に進めず困っています。 どうか解決方法を教えてください。 よろしくお願い致します。 ●ViewController.h// // ViewController.h // CameraApp // #import @interface ViewController : UIViewController - (IBAction)pressCameraButton:(id)sender; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end ●ViewController.m // // ViewController.m // CameraApp // #import “ViewController.h” @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. } - (IBAction)pressCameraButton:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [[self presentViewController:picker animated:YES completion:nil];} - (void)imagePickerController:(UIImagePickerController*)Picker didFinishPickingMediaWithInfo:(NSDictionary*)info { [self dismissViewControllerAnimated:YES completion:nil]; UIImage *originalImage = [info objectForkey:UIImagePickerControllerOriginalImage]; // ↑↑ 上記の行でエラーが出ます self.imageView.image = originalImage; } @end

  • Objective-Cのメモリ管理

    はじめまして。 iphoneアプリを作っていてメモリ管理について不明な点があるので教えて下さい。 memoryViewController.h ============================================== #import <UIKit/UIKit.h> @interface memoryViewController : UIViewController { id obj; } @property(retain)id obj; @end =============================================== memoryViewController.mm(一部) =============================================== @implementation memoryViewController @synthesize obj; - (void)viewDidLoad { [super viewDidLoad]; self.obj = [[NSObject alloc] init]; NSLog(@"count = %d", [obj retainCount]); } =============================================== 下記の部分についてです。 self.obj = [[NSObject alloc] init]; 「self.」を付けた場合retainCountは2で、付けなかった場合retaionCountは1になります。 何故違ってくるのかが理解できません。 教えて下さい。

  • objective-C(Xcode)に関して質問があります。

    objective-C(Xcode)に関して質問があります。 新規プロジェクトからview-base Applicationテンプレートを使用してプロジェクト(TestClass.xcodeproj)を作成します。 そうすると、TestClassAppDelegate.m, TestClassAppDelegate.h, TestClassViewController.h, TestClassViewController.mが Classesフォルダーに作成されます。このプロジェクトに自分で作成するクラス(MyTestClass)を追加するにはどうすれば良いのでしょうか? 下記のコードではエラーはでませんが、tc.counterにint 10が代入さず、0(nil)が出力されます。 (interfacebuilder でボタンのみ設置、ボタンを押すとpushが呼ばれ、logを残す) ===MyTestClass.h===== #import <Foundation/Foundation.h> @interface TestClass : NSObject { int counter; } @property int counter; @end ===MyTestClass.m===== #import "TestClass.h" @implementation TestClass @synthesize counter; -(int)calcPlus{ return counter = counter+10; } @end ===TestClassViewController.h==== #import <UIKit/UIKit.h> #import "TestClass.h" @interface TestClassViewController : UIViewController { TestClass *tc; } -(IBAction) push; -(void)calc; @end ===TestClassViewController.m=== #import "TestClassViewController.h" #import "TestClass.h" @implementation TestClassViewController -(IBAction)push{ tc.counter = 10; NSLog(@"%d", tc.counter); } サンプルコード(iPhoneUnitTests ((calc)))をみても、特別なことはなく、ヘッダーの読み込みさえ出来てれば クラスが使えそうなのですが・・・。 コメントをいただければ、幸いです。

  • objective-cで困っています教えてください

    objective-cをトライ&エラーで試しながら学習をしている初心者です。タッチした所を中心にまず100×100の正方形を表示し、その後スライダーで正方形の大きさを決めてから画像のトリミングをしたいのですが、正方形が表示されないのと、タッチした位置と切り取られる位置がずれています。上記の内容で行き詰っており、知恵をお借りしたいと思い投稿しました。宜しくお願いします。 ViewController.mの内容 #import "ViewController.h" @interface ViewController (){ // UILabel *label; // UIImageView *uv; //中心の位置 float v; // X座標 float x; // Y座標 float y; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //写真の表示 CGRect rect_uv = CGRectMake(0,20, 360, 360); UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect_uv]; //画像のアスペクト比を維持しUIImageViewサイズに収まるように表示 imageView.contentMode = UIViewContentModeScaleAspectFit; UIImage *image = [UIImage imageNamed:@"hoge.png"]; [imageView setImage:image]; [self.view addSubview:imageView]; // UISliderのインスタンス化 CGRect rect_sd = CGRectMake(50, 400, 200, 10); UISlider *slider = [[UISlider alloc]initWithFrame:rect_sd]; // スライダーの最小値,最大値,値を設定 slider.minimumValue = 0; slider.maximumValue = 500; slider.value = 100; // スライドしている最中に値を調べられるようにする.デフォルトでYESだがサンプルのため slider.continuous = YES; // スライダーの値が変更されたときに呼ばれるメソッドを設定 [slider addTarget:self action:@selector(slider_ValueChanged:) forControlEvents:UIControlEventValueChanged]; // スライダーをビューに追加 [self.view addSubview:slider]; // ラベルの設置 CGRect rect_lb = CGRectMake(300, 400, 100, 30); label = [[UILabel alloc]initWithFrame:rect_lb]; [self.view addSubview:label]; // 標準ボタン例文 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(50, 450, 100, 30); [btn setTitle:@"切り取り" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(trim:) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /** * スライダーの値が変更されたとき */ - (void)slider_ValueChanged:(id)sender { UISlider *slider = sender; // スライダーの値をログに記述 NSLog(@"スライダーの値:%f", slider.value); // ラベルのテキストを設定 //float v = slider.value; v = slider.value; label.text = [NSString stringWithFormat:@"%2.f",v]; //四角形を描画 CGContextRef context = UIGraphicsGetCurrentContext(); // コンテキストを取得 CGContextStrokeRect(context, CGRectMake(x,y,v,v)); // 四角形の描画 } /** * タッチされたとき */ - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.anyObjectメソッドでいずれか1つのタッチを取得 // 2.locationViewメソッドで対象となるビューのタッチした座標を取得 CGPoint p = [[touches anyObject] locationInView:self.view]; x = p.x; // X座標 y = p.y; // Y座標 NSLog(@"タップ開始 %f, %f", x, y); //四角形を描画 CGContextRef context = UIGraphicsGetCurrentContext(); // コンテキストを取得 CGContextStrokeRect(context, CGRectMake(x,y,100,100)); // 四角形の描画 } /** * 切り取りボタンが押されたとき呼ばれるtrimメソッド */ -(void)trim:(UIButton*)button{ // ここに何かの処理を記述する // (引数の button には呼び出し元のUIButtonオブジェクトが引き渡されてきます) // 切り抜き元となる画像を用意する。 //UIImage *srcImage = /* UIImagePickerなどから取得したUIImage */ UIImage *srcImage = [UIImage imageNamed:@"hoge.png"]; // 切り抜く位置を指定するCGRectを作成する。 CGRect trimArea = CGRectMake(x, y, v, v); // CoreGraphicsの機能を用いて、 // 切り抜いた画像を作成する。 CGImageRef srcImageRef = [srcImage CGImage]; CGImageRef trimmedImageRef = CGImageCreateWithImageInRect(srcImageRef, trimArea); UIImage *trimmedImage = [UIImage imageWithCGImage:trimmedImageRef]; // 画像表示例文 //UIImage *img = [UIImage imageNamed:@"hoge.png"]; UIImageView *iv = [[UIImageView alloc] initWithImage:trimmedImage]; [self.view addSubview:iv]; } @end

  • [objective-C]配列について

    はじめまして。 objective-C、cocos2dについての質問です。 iPhoneアプリを勉強しながら作り始めたのですが詰まってしまい、できればご助力をお願いしたいと思い質問させていただきました。 cocos2dというフレームワークを使用し簡単なRPGを作りたいと考えております。 そこでキャラクターのステータスを配列に格納し、どのシーンからも参照したいのですが、どうしてもうまくいきません。 別クラスでインスタンス変数を作り、それを利用して配列を作成する方法で実装しようとしており、実際のコードは以下の通りになります。 ---------------------------------- <Character.h> @interface Character : NSObject{ NSInteger characterStateAtk_; ・(Def、Hpなど) ・ } @property (nonatomic, assign) NSInteger characterStateAtk;  ・  ・ @end <Character.m> #import "Character.h" @implementation Character @synthesize characterStateAtk = characterStateAtk_;  ・  ・ @end キャラクターステータス生成部分 <CreateScene.h> @interface CreateScene : CCScene { NSMutableArray* characterStateArray_; } @property (nonatomic, assign) NSMutableArray *characterStateArray; @end <CreateScene.m> @interface CreateScene() - (void) pressReloadButton:(id)sender; - (void) createCharacterState; @end @implementation CreateScene @synthesize characterStateArray = characterStateArray_; - (void) dealloc{ [self.characterStateArray release]; [super dealloc]; } - (void) createCharacterState{ self.characterStateArray = [NSMutableArray array]; Character* priState = [[[Character alloc]init] autorelease]; priState.characterStateAtk = random() % 5 + 5; ・ ・ [self.characterStateArray addObject:priState]; } (その他の処理) - (void) pressReloadButton:(id)sender{ [self createCharacterState];  (ここでcharacterStateArrayを取得して表示したい) } ---------------------------------- 最初は、 NSInteger x; x = [[self.characterStateArray objectAtIndex:0]intValue]; で取得しようとしたのですがエラーとなり実行できませんでした。 (調べていくうちにobjective-Cでは配列の中身はオブジェクトしか使えないという事を知りました) プログラム中の(ここでcharacterStateArrayを取得して表示したい)の部分について、実装方法や考え方についてご助言をいただけないでしょうか? よろしくお願いいたします。