1

エレベーターの物を作っています。presentModalViewController を使用して異なるビューでデータを送信するのに問題があります。「favoriteColorString」プロパティが見つからないという赤いメッセージが表示されました。まったく同じですが、異なるフォーム名とボタンをコピーしました。「favoriteColorString」にエラーが表示され、エレベータ 2 データを送信できません。

私は2つの異なることを試しました。

  Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

  favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

これが私のコードです:

ElevatorView.h

#import <UIKit/UIKit.h>
#import "Elevator2View.h"

@interface ElevatorView : UIViewController<PassSecondColor>
 {

Elevator2View   *Elevator2View;

IBOutlet UITextField     *favoriteColorTextField;
IBOutlet UILabel         *favoriteColorLabel;
IBOutlet UILabel         *secondFavoriteColorLabel;

NSString        *secondFavoriteColorString;

}

@property (nonatomic, retain) Elevator2View *Elevator2View;
@property (nonatomic, retain) IBOutlet UITextField  *favoriteColorTextField;
@property (nonatomic, retain) IBOutlet UILabel      *favoriteColorLabel;
@property (nonatomic, retain) IBOutlet UILabel      *secondFavoriteColorLabel;

@property (copy) NSString   *secondFavoriteColorString;

@end

ElevatorView.m

#import "ElevatorView.h"
#import "Elevator2View.h"
    @implementation ElevatorView
@synthesize Elevator2View, favoriteColorTextField, favoriteColorLabel, secondFavoriteColorLabel;
@synthesize secondFavoriteColorString;



-(IBAction)level1:(id)sender;{
   favoriteColorTextField.text = @"1";
      Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

      [self presentModalViewController:[[[Elevator2View alloc] init]
                                  autorelease] animated:NO];
}

Elevator2View.h

#import <UIKit/UIKit.h>

@protocol PassSecondColor <NSObject>
@required
- (void) setSecondFavoriteColor:(NSString *)secondFavoriteColor;
@end

@interface Elevator2View : UIViewController{
IBOutlet UITextField *secondFavoriteColorTextField;
IBOutlet UILabel     *favoriteColorLabel;
IBOutlet UILabel     *secondFavoriteColorLabel;
NSString             *favoriteColorString;
id <PassSecondColor> delegate;

}

@property (copy) NSString   *favoriteColorString;



@property (nonatomic, retain) IBOutlet UITextField  *secondFavoriteColorTextField;
@property (nonatomic, retain) IBOutlet UILabel      *favoriteColorLabel;
@property (nonatomic, retain) IBOutlet UILabel      *secondFavoriteColorLabel;
@property (retain) id delegate;

@end

Elevator2View.m

#import "Elevator2View.h"

@interface Elevator2View ()

@end

@implementation Elevator2View
@synthesize secondFavoriteColorTextField, favoriteColorLabel, secondFavoriteColorLabel;
@synthesize favoriteColorString;
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       // Custom initialization
   }
   return self;
}

- (void) viewWillAppear:(BOOL)animated
{
    favoriteColorLabel.text = favoriteColorString;  
}

- (void) viewWillDisappear:(BOOL) animated
{
//  [[self delegate] setSecondFavoriteColor:secondFavoriteColorTextField.text];
}


- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.
    favoriteColorLabel.text = favoriteColorString;  
}

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

http://www.theappcodeblog.com/?p=90を参照してください。

4

1 に答える 1

0

「プロパティが見つかりません」の理由は、ivar にクラスと同じ名前を付けたことです。ドット表記は構文糖衣にすぎません:object.property = valueは と同等[object setProperty:value]です。Objective C ではクラスもオブジェクトであり、 を呼び出すと、Xcode はどうやらクラス Elevator2View のクラスメソッドElevator2View.favoriteColorString = whateverを呼び出そうとしていると見なし ます。setFavoriteColorString

このエラーを取り除くのは簡単です。ivar の名前を別の名前に変更するだけElevator2View *Elevator2Viewです。実際、Xcode 4.4 以降では、プロパティの ivar が自動合成されます。プロパティがある場合propertyName、Xcode は ivar を自動合成し_propertyNameます。あなたの財産Elevator2Viewには_Elevator2Viewivarがあります。そのため、本当に別の命名スキームで ivar が必要な場合を除き、 を取り除くことができます@synthesize。また、プロパティの ivar を宣言する必要もありません。

(プロパティの ivar を宣言することを好みますが (Xcode の命名スキームに従います)、lldb はオブジェクト インスペクターで宣言なしで自動合成された ivar を表示しないことがあまりにも多いためです。)

それは、プロパティ、ivar、および命名規則に関するものでした。しかし、このコードで何をしているのでしょうか?

-(IBAction)level1:(id)sender;{
   favoriteColorTextField.text = @"1";
      Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

      [self presentModalViewController:[[[Elevator2View alloc] init]
                                  autorelease] animated:NO];
}

の値を設定しますElevator2View-インスタンス変数の-プロパティ、次にElevator2View クラスの新しいオブジェクトを作成し、それをモーダルビューコントローラーとして提示します。(ちなみに、presentModalViewController:animated:iOS 6.0 では非推奨です)。もちろん、この真新しいオブジェクト は(インスタンス変数の) プロパティがElevator2View何であるかを知りません!Elevator2View

于 2013-04-11T07:19:37.017 に答える