1

I'm making elevator thing. I'm having trouble sending data with different views using presentModalViewController. I got red message "favoriteColorString" property not found. I copied exactly the same but different form names and buttons. The "favoriteColorString" appears an error and unable to send elevator2 data.

I tried two different thing.

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

And

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

Here's my code:

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

See http://www.theappcodeblog.com/?p=90

Dmitry Pashkevich
  • 13,431
  • 9
  • 55
  • 73
HotScreen
  • 11
  • 2
  • 1. post `Elevator2View`code. 2.Even if you don't care about naming conventions, please _don't_ name your variables same as classes. – Kreiri Apr 10 '13 at 18:55
  • Here's the code for Elevator2Viewcode. – HotScreen Apr 10 '13 at 19:39
  • also (adding to @Kreiri's comment) when using properties, don't also declare iVars, and don't synthesize. Always refer to them using property syntax self.property. [See here](http://stackoverflow.com/questions/14236799/should-i-declare-variables-in-interface-or-using-property-in-objective-c-arc/14236931#14236931) for details. It would help if you cleaned up your question with these recommendations. – foundry Apr 10 '13 at 21:07

1 Answers1

0

The reason for your "property not found" is that you named your ivar same as class. Dot notation is just a syntactic sugar: object.property = value is equivalent to [object setProperty:value]. In Objective C classes are also objects, and when you call Elevator2View.favoriteColorString = whatever, Xcode apparently thinks that you are attempting to call class method setFavoriteColorString of class Elevator2View.

Getting rid of this error is easy: just rename your ivar Elevator2View *Elevator2View to something else. In fact, Xcode 4.4 and newer autosynthesizes ivars for your properties: if you have a property propertyName, then Xcode will autosynthesize ivar _propertyName. Your property Elevator2View will have _Elevator2View ivar. So unless you really really need to have ivars with different naming scheme, you can get rid of your @synthesize, and you also don't need to declare ivars for you properties.

(Though I prefer to declare ivars for properties (following Xcode naming scheme), because far too often lldb doesn't show autosynthesized-without-declaring ivars in object inspector.)

That was about properties, ivars and naming conventions. But what are you doing in this code?

-(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];
}

You set value of Elevator2View's - your instance variable's - property, then create brand new object of Elevator2View class and present that as modal view controller. (By the way, presentModalViewController:animated: is deprecated in iOS 6.0). Of course, this brand new Elevator2View object has no idea what Elevator2View's (your instance variable's) properties are!

Kreiri
  • 7,840
  • 5
  • 30
  • 36