0

I have two views, and i'm trying to show text that i getting from first view in UITextField of another . Second view shown by - (source) so methods ViewWillAppear and ViewDidLoad won't work. And viewDidLoad method of second view is runs when app is started.

I'm tried to make method of second class

secondClass.h:

@property (strong, nonatomic) IBOutlet UITextField *itemName;//all hooked up in storyboard
-(void)SetName:(NSString *)name;

secondClass.m:

-(void)SetName:(NSString *)name{
    NSLog(@"%@",name);
    itemName.text = name;//itemName - textField
}

and use it in first one:

secondViewConroller *secondView = [[secondViewConroller alloc]init];
[secondView SetName:@"Bill"];

NSlog shows "Bill" but textField.text won't change anything.

My guess that app shows UITextField without changes because it shows second view that it gets from viewDidLoad method and i need to update it somehow

My question: What is the best approach to change attributes of UI elements from different classes?

Vlad Z.
  • 3,401
  • 3
  • 32
  • 60

4 Answers4

0

Easiest way:

secondViewConroller.h :

@property NSString * conversationName;

secondViewConroller.m :

@synthesize conversationName;

-(void)SetName:(NSString *)name{
    NSLog(@"%@",name);
    itemName.text = conversationName
}

On alloc:

secondViewConroller *secondView = [[secondViewConroller alloc]init];
conversationName = @"Set this text";
[secondView SetName:@"Bill"];

I would suggest you to read about Protocols after that.

Dark Matter
  • 3,638
  • 3
  • 14
  • 14
0

Easiest way: in secondViewConroller.h :

@property (nonatomic, retain) NSString *stringName;

secondViewConroller.m :

@synthesize stringName;

and in viewDidLoad method you write this line

itemName.text = stringName

On alloc:

secondViewConroller *secondView = [[secondViewConroller alloc]init];
secondView.stringName = @"Set this text";
Waseem Shah
  • 2,219
  • 23
  • 23
0

guess there is something wrong with your itemName variable if it appears to get to the nslog. did you create a referencing outlet in the interface builder for the textfield? otherwise you can get the right textfield by tag, in IB put for instance tag 1 on the textfield and do in code:

UITextField *tf=(UITextField*)[self.view viewWithTag:1];
tf.text=name;

(replace self.view for the view holding the textfield, if not directly in the main view)

karel
  • 63
  • 5
  • text field is working if i'm changing it in second class (where it's created). The problem is with changing it from first class – Vlad Z. Feb 13 '13 at 15:23
  • btw, you now have a typo in your question: SetName and SetItemName, one or the other, but not both ;-) – karel Feb 13 '13 at 17:18
0

So i found a solution: There's was something wrong with calling method SetName: with parameters that i getting from first UIViewController.

Basically the solution is : create NSObject and put in there value from first UIViewConroller and then use it in second.

This TUTORIAL helped me to resolve the problem.

Vlad Z.
  • 3,401
  • 3
  • 32
  • 60