problem in displaying a string in a textbox - iphone

Hi to all,
I passed a variable from first.m to seViewController.m. I'm able to print that variable using NSLog(#variable) but I'm unable to use textField.text=variable. How to print that variable in a textbox?
First.m
-(void)buttonPressed01
{
seViewController *seView = [[seViewController alloc] init];
[seView insert:myString];
[self.navigationController popViewControllerAnimated:YES];
}
seviewcontroller.m
-(void)insert:(NSString*) myString
{
NSLog(#"%#",myString);
textField.text=[NSString stringWithFormat:#"%#",myString];
}

seViewController *seView = [[[seViewController alloc] initWithNibName:#"seViewController" bundle:nil] autorelease];
[seView insert:String];
[[self navigationController] pushViewController:seView animated:YES];
seviewcontroller.m
-(void)insert:(NSString*) myString
{
NSLog(#"%#",myString);
textField.text=myString;
}

Until your seViewController is loaded you can not use its outlets, here you are using textField. It won't set text because after seViewController *seView = [[seViewController alloc] init]; the view is initialized but still you need to load its view in memory in order to use its outlet. so if you are using its view then after addingSubview, or presenting modalView or, pushing it on navigation stack call that method.
or else you should have a string in seviewcontroller which you will set and when controller's will be loaded in memory viewDidLoad just do what you are doing right now in insert method, set TextField's text.
Also, I am not sure what you want to do with seView? why you are creating new instance and not using it ? May be you have created this controller earlier and you want to change that controller's textField text not the new one. Then in that case you should refer that controller not this one because its totally different object.
Thanks

When you pop from stack it loads the instance of the view controller you pushed in to stack. Here you created a new instance and set the text field and loads the instance in the stack. Thats why text field does not show the value to set. If you want to pass the value to the class you nee to pass the value using some singleton class or app delegate.

I think its not working because you are passing this variable to a new instance of your seViewController while you are popping it in the next line. You have to get the same instance as -
seViewController *seView = (seViewController*)[self parentViewController];
[seView insert:myString];
[self.navigationController popViewControllerAnimated:YES];

You can use this one if what I understood is correct,
you can retrieve the array of viewControllers from the navigation controller.
You dont need to initialize the view controller and this wont work as explained above.

Related

passing a string value from one view controller to a second variable view controller Xcode

I have an app in which I am loading variable view controllers depending on where the user is in the app. This is my code.
-(IBAction)buttonPressed:(id)sender;{
if (mission <1) {
gameViewController *detailViewController = [[gameViewController alloc] initWithNibName:#"gameViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
detailViewController.which2 = which;
}
else if (mission > 0) {
NSString *viewController = #"gameViewController";
NSString *missionViewController = [viewController stringByAppendingString:missionNo];
Class controllerClass = NSClassFromString (missionViewController);
id detailViewController = [[controllerClass alloc] initWithNibName:#"gameViewController" bundle:nil];
NSLog(#"missionViewController;%#",missionViewController);
[self.navigationController pushViewController:detailViewController animated:YES];
detailViewController .which2 = which;
}
}
everything work great except I want to pass a string from the first view controller to the second view controller which ever one that may be.
As you can see I have put in the code just above detailViewController.which2 = which;
I have created the property and synthesized NSString *which in my first view controller and NSString *which2 in all the subsequent view controllers. in the first instance where mission is <1 everything works ok and NSLog shows the string being passed. However with the second detailViewController (which is the variable view controller) I get the error Property 'which2' not found on object of type"_strong id' Does anyone have any suggestion on how to resolve this?
the other viewControllers are gameViewController1, gameViewController2, etc. Each is rather long and complex. But they all load into the same xib file gameViewController. There is a UIlabel that update to one higher once the user finishes that gameView so they can go on the the next on in the series or go back to the main menu. If they go back to the main menu the number is added to "gameViewController" so the correct one is loaded. So I can't specify which view controller is going to load since it depends on the user's place. Thus the missionViewController with the # of mission added to load the correct view controller. Each of the subsequent view controllers has a which2 created and synthesized. What if we pretend that all subsequent view controllers just had a UILabel that is going to display the string "which2 in it. All I want to do is pass the string "which" to the next viewController (whichever one that is) as "which2".
Dynamic binding allows you to send messages to an id as long as the selector exists in the project, but dot-syntax is not allowed.
Changing
detailViewController.which2 = which;
to
[detailViewController setWhich2:which];
should suppress the warning.
write this : `detailViewController .which2 = which;
just before you push navigation controller.`
UPDATE:
Use Protools to update the value.
#protocol MissionProtocol
#required
-(void) updateValue:(NSSTring*) value;
#end
Implement the protocols in your ViewControllers. i.e.
#interface MissionViewController:UIViewController<MissionProtocol>
....
#end
In your implementation file, implement the method updateValue.
-(void) updateValue:(NSString*) value
{
self.which2=value;
}
Then change your original code to:
NSString *viewController = #"gameViewController";
NSString *missionViewController = [viewController stringByAppendingString:missionNo];
Class controllerClass = NSClassFromString (missionViewController);
id<MissionProtocol> detailViewController = [[controllerClass alloc] initWithNibName:#"gameViewController" bundle:nil];
[detailViewController updateValue:which];

NSEntityDescription insertNewObjectForEntityForName returns NSManagedObject just deleted

- (void)addPlace {
//set up add controller
PlacesAddViewController *addController = [[PlacesAddViewController alloc] initWithStyle:UITableViewStyleGrouped];
addController.delegate = self;
// create new place and assign it to add controller
Place *newPlace = [NSEntityDescription insertNewObjectForEntityForName:#"Place" inManagedObjectContext:self.managedObjectContext];
addController.place = newPlace;
// set up navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];
// release controllers
[navigationController release];
[addController release];
}
The method insertNewObjectForEntityForName is returning a new instance of Place correctly when I try adding a place initially, but when I delete a place from the list then press "+" this method is called again and this time insertNewObjectForEntityForName returns the place that was just deleted.
Can anyone give me some ideas as to what is happening? I know this method returns an autoreleased object -- not sure if this has anything to do with it.
UPDATE: I think it's happening because right before the add view controller displays you can see the new object being inserted at the top of table view controller in the first row. When the add view controller is displayed the new object is indeed a new instance. Also, if I save this object it correctly replaces the row in the table view controller with the correct information. So maybe this has something to do with my table view below. Ahhh, I just figured it out and answered my own question. My cellForRowAtIndexPath was using a reused cell and the information was displaying the latest cell deleted. I've since fixed this. Thanks though for the reassurance it wasn't the code above.
That can't be happening given the code above. Why do you think that is what's happening? Add NSLog statements to output properties of newPlace after it has been inserted and before you present the navigationController to verify this. I would also do some logging in addController. Whatever is going on, the problem is likely there. What is the #property definition for the place property in addController? Add some more code and I'll try to help more.

reloadData for a string

I have this in one view:
-(void)viewWillDisappear:(BOOL)animated
{
RootViewController *rVC = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
[rVC setMessage:[label text]];
NSLog(#"ihere - %#",rVC.message);
}
The NSLog returns the correct string. How would I reload the data in the RootViewController to update the string message there?
doing this doesn't work in my RootViewController (which i go back to in navcontroller):
-(void)viewWillAppear
{ [[self message] reloadData]; }
because the message is just a string. Can somebody show me how to fix this please?
Hi can someone else try to help me please?
In the viewWillAppear event, i need to reloadData on a NSString. So i need to convert it somehow to an object before i can use reloadData on it.
That's because NSString doesn't have a reloadData method.
And as it is immutable it wouldn't make sense if it did.
What you probably want to do is display your string in viewWillAppear and change the model property in the controller where it gets this from.
Delegation is the usual way to do this and I've written a couple of examples that might help you see what is happening;
DelegationExample
TableViewDelegation
Presuming you're using a NavigationController: Consult your navigation controller's stack (property navigationController in your current controller) to access the element for your root view controller (this would generally be the zeroth element -- use viewControllers). Set your message property using that pointer. Then be sure that your root view controller, in viewWillAppear, uses the property you just set to reset the text of the label of interest (using a simple assignment statement -- self.myLabel.text = self.message;.

Set another class' property

I want to set an NSString property of a main view from a nested view. I do so right now by allocating the previous class and accessing the class.property. When I pop the view controller programmatically and NSLog the property from the main view, it's null.
How does this happen?
EDIT:
MainViewController *controller = [[MainViewController alloc] init];
switch (indexPath.row) {
case 0:
controller.category = #"Categorie 1";
break;
default:
break;
}
[controller release];
You mentioned you are going to pop the viewController so I assume you are trying to set a property of the controller below the navigation stack.
Instead of creating a new object of the class, you should get back the original object that was already created.
NSArray *viewControllers = [self.navigationController viewControllers]; // array of viewControllers currently on the navigation stack.
MainVC *mainVC = (mainVC *)[viewControllers objectAtIndex:viewControllers.count - 2];
[mainVC setProperty:...];
Well first of all, you cannot access a specific instance's properties by simply calling the class. The class has no connection to any specific instance of it.
You need to have an instance variable in your nested view that references the parent. set this up when you create it. Then when you are in the child view controller you can still access the parent.
Something like this in the child:
MyParentViewController *parentVC;

pushViewController, when to set UILabel text, and does setNeedsDisplay need to be called?

I ran into something odd today that maybe someone knows something about. I have a subclass of UIViewController and its associated NIB. I set the labels in the UIViewController methods and all that works fine.
Now from another class, I create that ViewController again because I want to reuse it. I do this:
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
vc.titleLabel.text = #"testing";
vc.myTextLabel.text = #"yo";
self.navigationController pushViewController:vc animated:NO];
[vc release];
This does NOT work. I have no idea why this does not work. I would think I would set all the labels, then show the view controller by pushing it onto the stack.
However, if I do this:
[vc.view setNeedsDisplay]; // why here???
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
vc.titleLabel.text = #"testing";
vc.myTextLabel.text = #"yo";
self.navigationController pushViewController:vc animated:NO];
[vc release];
This DOES work. This does not make sense to me. I thought setNeeds Display was called AFTER a view needs to be redrawn. If I move setNeedsDisplay to the end of the block it does NOT work. It only works at the beginning of the block which is very odd to me. Any one encounter this before or know why it works this way? Thanks.
The reason is that a view controller's view is lazily-loaded. This means the controller's view is only loaded from a nib (or via -loadView) when you access the view property for the first time. If you attempt to access the labels before the view has been loaded, they will be nil and any messages you send to them will be no-ops.
So to force the view to load, you can do this:
/* make sure the view is loaded */
[vc view];
/* Access the label properties */
vc.titleLabel.text = #"testing";
However, forcing the view to load may not be a good idea in all situations, especially if the view controller is not going to be displayed immediately and you want to save memory.
In this case you can create the labels in the controller's init method so they always exist, and add them to the view controller's view manually in -viewDidLoad, rather than in your nib. This will allow the standard lazy-loading behaviour to work, but users of your class can still set properties on the labels before the view is loaded.
An alternative is to expose simple NSString properties with associated ivars on the view controller to represent any titles or text in the view. Then in your -viewDidLoad you can set the text of the labels to the value of these properties. Users of your view controller can then set these properties before the view has loaded.