• 締切済み

objectC

こんにちは。最近object-cの勉強を始めてわからないことがあったので、質問させていただきます。 2つの数をそれぞれ入力して、それらを掛けて、出力するプログラムを作りたいのですが、 下記のようにひとまずざっとつくってみたのですが、うまくできなくて困ってます。どのようにつくればいいのでしょうか??xとyは入力用です。上段がhファイル、下段がmファイル @interface ViewController : UIViewController{ IBOutlet UITextField *x; IBOutlet UITextField *y; IBOutlet UITextField *result; } -(IBAction)convert; @end @implementation -(IBAction)convert{ int a,b,c; a = [x.text intValue]; b = [y.text intValue]; c = a*b; result.text = c; }

みんなの回答

  • jjk65536
  • ベストアンサー率59% (66/111)
回答No.1

result.text = c; ここは間違っていますね。 result.textはNSString型ですので、int値の代入はできないです。 int値をNSStringにするには、一般的に以下のどちらかで行います。 result.text = [[NSNumber numberWithUnsignedInt:c] stringValue]; result.text = [NSString stringWithFormat:@"%d", c]; あと、Objective-Cです。

関連するQ&A

  • swift初心者で、計算機を作っています

    swiftで計算機を作っています。 しかし、エラーが出てしまいます nilを代入するのがいけないらしいことは分かったのですが、ではどうしたらよいのかいろいろ調べていろいろ行ったのですが(変数への代入をボタンの関数内したり、変数宣言をstring?にしたり、計算にnumberBox.textを直接キャストしたものを用いる、などなど)、どうしても治りません。 この記述をこう直せばいい、という回答をお願いします。 import UIKit class ViewController: UIViewController { @IBOutlet weak var result: UILabel! @IBOutlet weak var numberBox1: UITextField! @IBOutlet weak var numberBox2: UITextField! var num1:String = "" var num2 = 0 var num3:String = "" var num4 = 0 var res = 0 @IBOutlet weak var button1: UIButton! @IBAction func button1(sender: AnyObject) { add() } @IBOutlet weak var button2: UIButton! @IBAction func button2(sender: AnyObject) { sub() } @IBOutlet weak var button3: UIButton! @IBAction func button3(sender: AnyObject) { mul() } @IBOutlet weak var button4: UIButton! @IBAction func button4(sender: AnyObject) { div() } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. result.text = "計算結果" num1 = numberBox1.text! num2 = Int(num1)! num3 = numberBox2.text! num4 = Int(num3)! button1.setTitle("+", forState: .Normal) button2.setTitle("-", forState: .Normal) button3.setTitle("✖️", forState: .Normal) button4.setTitle("➗", forState: .Normal) } func add(){ res = num2 + num4 result.text = String(res) } func sub(){ res = num2 - num4 result.text = String(res) } func mul(){ res = num2 * num4 result.text = String(res) } func div(){ res = num2 / num4 result.text = String(res) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } エラーメッセージ fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) 回答をよろしくお願いします

    • ベストアンサー
    • Swift
  • Objective-C ボタンを動かすには

    Objective-Cはまったくの初心者で、iPhone用のアプリを作るのに書籍を見ながらサンプルをいじっています。 画像をタップしたらその画像自体が動くようなものを作りたいのですが、 RoundRectButtonを1個だけ配置して 【imageTestViewContollorer.h】 @interface imageTest1ViewController : UIViewController { IBOutlet UIButton *myButton; } -(IBAction)tapBtn; @end 【imageTestViewContollorer.m】 @implementation imageTestViewController -(IBAction)tapBtn{ myButton.center=CGPointMake(50,50); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:2]; myButton.center=CGPointMake(160, 240); [UIView commitAnimations]; } としてみましたが、コンパイルエラーなどはありませんでしたが、ボタンを押しても何も起こりませんでした。 IBOutlet UIButtonが臭いような気もしているのですが、UIImageViewにするとRoundRectButtonと線を繋げなくなりますし、どう直してよいか見当もつかず困っています。 画像がタップされたら動く、みたいなものをそもそもRoundRectButtonに画像を割り当てた方がいいのかImageViewにタップされた場合の処理を割り当てた方がいいのかどうかもよくわかりません。 よろしくお願いいたします。

  • iosプログラムで背景色をコードで変更するには?

    xcodeでiphone用のアプリを作りたいと思い、プログラミングをやっています。 ラベルボタンを押したら背景色を変更するようなプログラムをしたいと思っています。 class ViewController : UIViewController { @IBOutlet weak var textfield: UITextField! @IBAction func sayhello(sender: AnyObject) { textfield.text = "Hello world!" } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } このhello worldをやった時に作成したプロジェクトを使用して、ボタンを押したら背景を任意の色(今回は黒)に変えたいと思っています。 それでこのクラス関数内にMystorlyboardウィンドウから背景を"control"ボタンをクリックしながら @IBOutlet var background: UIView! というのを追加しました。 そして、押すと"hello world"とテキストボックスに表示するボタンの処理の中に background.backgroundColor = Black としてみたのですが、エラーが出てビルドが成功しません。 (エラー内容)”Use of unresolved identifier ''Blue' この場合どのようにコードを修正するべきでしょうか? どうぞ、ご教示頂きますよう、よろしくお願い致します。 (現在のコード内容) import UIKit class ViewController: UIViewController { @IBOutlet var background: UIView! @IBOutlet weak var textfield: UITextField! @IBAction func sayhello(sender: AnyObject) { textfield.text = "Hello world!" background.backgroundColor = Blue } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

  • swift データの保存、読み込みについて

    swiftのデータ保存と読み込みについて学習しています。 saveボタンを押すとテキストフィールドの文字が保存され、switchボタンを押すと保存された文字列が表示され、文字列が表示されている場合はその文字列を消去するプログラムを書いたのですが、実行してみるとデータ保存がうまくいっていないようでした。 どこに問題があるのでしょうか。回答をお願いします。 以下ソースコードです import UIKit class ViewController: UIViewController { @IBOutlet weak var word1: UITextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func save1(sender: AnyObject) { let ud1a = NSUserDefaults.standardUserDefaults() ud1a.setObject(word1.text, forKey: "saveText1") } @IBAction func switch1(sender: AnyObject) { let ud1b = NSUserDefaults.standardUserDefaults() if(word1.text == nil){ var loadText1:String! = ud1b.stringForKey("saveText1") word1.text = loadText1 } else{ word1.text = nil } }

  • swift言語 Xcode

    アプリを作る勉強をしています。参考書通りやりましたが、警告が出てしまいます。 原因がわからず困っています。どなたか助けて頂けませんでしょうか?宜しくお願い致します。 ソースはこちらです。 import UIKit import MapKit class ViewController: UIViewController ,UITextFieldDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. inputText.delegate = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBOutlet weak var inputText: UITextField! @IBOutlet weak var dispMap: MKMapView! } func textFildShoouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() let searchKeyword = textField.text print(searchKeyword) ⇦ここに警告が出ます。 return true

  • Obj-CでViewコントローラのラベルに表示する

    Objective-Cであるクラスから、特定のViewControllerのもつLabel要素へテキストを表示したく、色々試してみたのですが、うまく動作しませんでした。 詳しい方、ぜひアドバイスお願いします。 □やりたいこと クラス側から、ViewControllerの持つ要素を操作したい □テストで使用したコード test_buttonをクリックすると、lbl_mesへ文字列を表示する ----------------- ■classTest.h #import "helloViewController.h" ----------------- ■classTest.m #import "classTest.h" - (void) makeStr { helloViewController *hv = [[helloViewController alloc] init]; hv->lbl_mes.text = @"HELLO WORLD"; [hv release]; } ----------------- ■helloViewController.h #import "classTest.h" @interface helloViewController : UIViewController { @public IBOutlet UILabel* lbl_mes; } @property(nonatomic,retain) IBOutlet UILabel* lbl_mes; -(IBAction) test_button; ----------------- ■helloViewController.m #import "helloViewController.h" -(IBAction) test_button{ classTest *clst = [[classTest alloc] init]; [clst makeStr]; [clst release]; } よろしくお願いします。

  • objective-c 画面遷移について

    プログラミングに関して素人です。 UIViewControllerからPushコードを使って別のUIViewに画面遷移したいのですが 遷移先にTableViewを2つ並べております。 画面遷移する際のコードはどのように記述したらいいのでしょうか? 詳しいかたお力お貸しいただければと思います。 ViewController.m - (IBAction)NewsPage:(UIButton *)sender { //こちらの記述方法が不明 } TViewController.h // XibファイルとUIViewの紐づけ。 @property (weak, nonatomic) IBOutlet UITableView *leftTableView; @property (weak, nonatomic) IBOutlet UITableView *rightTableView; - (IBAction)TopButton:(UIButton *)sender; 根本的になにか違えばご指摘お願いします。

  • Objective-Cのエラーコードの意味

    タイトルの通りです。 エラー部分は最下部にあります。 ------------------------------------------------------------------------------------------------- // // ViewController.m // Kadai // // Created by on 2014/08/10. // Copyright (c) 2014年 saikoro. All rights reserved. // #import "ViewController.h" @interface ViewController () <UIAlertViewDelegate> @property (weak, nonatomic) IBOutlet UITextField *tfMyouji; @property (weak, nonatomic) IBOutlet UITextField *tfName; @property (weak, nonatomic) IBOutlet UILabel *Aisatsu; @end @implementation ViewController // 画面タッチ時 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // キーボード非表示 [self.tfMyouji resignFirstResponder]; [self.tfName resignFirstResponder]; } - (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)introduce:(id)sender { UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"確認" message:@"挨拶しますか?" delegate:self cancelButtonTitle:@"キャンセル" otherButtonTitles:@"はい", nil]; // 表示 [av show]; } // AlertViewボタン押下 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"再確認" message:@"本当に挨拶しますか?" delegate:nil cancelButtonTitle:@"やっぱりやめときます" otherButtonTitles:@"早く!", nil]; [av show]; } // AlertViewボタンさらに押下 + (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ // 入力された名前の保持 NSString *str = self.tfMyouji.text; ←ここでエラーが出ます。なぜか「tfMyouji」が                    予測で出てきません } @end ------------------------------------------------------------------------------------------------- どなたかエラーの理由をご教授頂けないでしょうか。 以上、何卒宜しくお願い致します。

  • View controllerのoutlet

    現在xcodeでswiftのプログラミングを行っています。 現在storyboardで複数のview controller(3つのview controllerを追加)を追加して、そのうちの1つの画面には現在時刻の表示するプログラムをしたいと思っています。 そこで、 リフレッシュのためにSwiftで時計を作った by @fantmsite on @Qiita http://qiita.com/fantm21/items/9b9abd472cfed0b5f883 こちらのサイトを参考にしてやってみようと思い、まず、 @IBOutlet weak var yearLabel: NSTextField! @IBOutlet weak var monthLabel: NSTextField! @IBOutlet weak var daysLabel: NSTextField! @IBOutlet weak var dayOfTheWeekLabel: NSTextField! @IBOutlet weak var hourLabel: NSTextField! @IBOutlet weak var minutesLabel: NSTextField! @IBOutlet weak var secondLabel: NSTextField! これらのOutletの登録を行う為、storyboardのView controllerに配置したlabelをcontrolボタンを押しながら、ViewController.swiftの画面の ”class ViewController: UIViewController {” 行の下側に引っ張ってみたのですが、outletを登録するのかとかの表示が全く出てきません。 いろいろといじってみてわかったことは 一番の最初のViewControllerのlabelやコマンドボタンなどはoutletの登録ができるのですが、2番目以降のViewControllerではこれができないことです。 どなたか解決方法をご教示いただきますよう、宜しくお願い致します。

    • ベストアンサー
    • Swift
  • 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

専門家に質問してみよう