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;.
Related
I have a little problem, because I can't pass data to another view controller.
-(void)barcodeData:(NSString *)barcode type:(int)type
{
mainTabBarController.selectedViewController=self;
[status setString:#""];
[status appendFormat:#"Type: %d\n",type];
[status appendFormat:#"Type text: %#\n",[dtdev barcodeType2Text:type]];
[status appendFormat:#"Barcode: %#",barcode];
[displayText setText:status];
codede1String = barcode;
NSLog(#"%#",codede1String);
[self updateBattery];
}
In the code above I put a barcode in the String codeString1. So far no problem, but when I send this data to the other viewController barCodeString suddenly is NULL.
I pass data to viewcontroller like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"barcode"]) {
main =[ segue destinationViewController];
main.code1.text = codede1String;
}
}
So how do I solve this problem?
I assume that code1 is a text field, text view or label. If so, the reason this doesn't work, is that the outlets for main are not yet set at the time of prepareForSegue. You need to create a string property in main and set its value to code1String in prepareForSegue. Then, in main's viewDidLoad or viewDidAppear, set code1's text value to that string.
Note that IBOutlets of the destination view controller are not set yet in prepareForSegue. So if main.code1 represents a UILabel, it will be nil because the view is not yet loaded. You should pass the string directly using a setter method: [main setCodeString:codede1String] and then configure the user interface in viewDidLoad: self.code1.text = [self codeString].
Do this in the viewDidLoad of the view controller, then it works fine
- (void)viewDidLoad
{
codedeString=[[NSString alloc] init];
}
Try doing this, it helped me once for the same kinda issue (but with xibs)
Add one more line, just before any assignments
[main.view setAlpha:1.0f];
main.code1.text = codede1String;
Hope this will help you as well.
- (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.
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.
I have been trying to add/implement this example to my existing Split View app tests.
Apple Example
I what to use the concept of replacing the detail view or right view, otherwise my app will be different. It is this difference that is causing my problems.
I have a rootviewcontroller or left view and upon choosing something here a new view is pushed onto this view. Upon choosing something in this "pushed view" I want to change the detail view or right hand view. This is the difference to apples example where the rootview does not have a pushed view on it and thus references are not broken.
Below is my change code - the new View DVCases is being initialized but the didload is not happening.
The issues are learner issues to do with my classes.
This below code is in my RootViewController implementation code but my reference to splitviewcontroller is not working if there is a new view pushed.
Second self.navigationcontroller is not correct because I have pushed a second view to the rootviewcontroller.
To centralize and simplify the code what I have done is from the delegate of the pushed view in the didselect event i call a method found in the rootviewcontroller passing the index as a parameter. The code for my custom method contains what is below.
So my question is how do I do this in my situation where I have pushed other views onto the rootview or left side. It appears that after pushing a view the reference to splitviewcontroller is gone and self.navigationcontroller is also gone/or wrong.
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
if (value == 0) {
DVCases *newDetailViewController = [[DVCases alloc] initWithNibName:#"DVCases" bundle:nil];
detailViewController = newDetailViewController;
}
// Update the split view controller's view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;
[viewControllers release];
// Dismiss the popover if it's present.
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}
// Configure the new view controller's popover button (after the view has been displayed and its toolbar/navigation bar has been created).
if (rootPopoverButtonItem != nil) {
[detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}
[detailViewController release];
I would appreciate any tips or help you might have.
Initialization of any viewcontroller class does not mean that it will make call to viewDidLoad method.
viewDidLoad method will only be called when you load view of that viewController. Generally we do it either by following methods.
1. Pushing it on navigation stack.
2. Presenting it using modal transition.
3. Adding it on some other view using [someView addSubView:controller.view];
4. Selecting any tabBar item for the first time Or tapping tabBar Item twice.
there may be some other scenarios.
But right now in your code I don't see any of this element.
Initialization means you are calling the direct method for intialization(calling its constructor) like here in above code initWithNibName will call this method of DVClass not any other(until this method had call for other methods inside it).
Thanks
As I am learning to properly code - my problems centres around that.
The above code is perfect as long as you call it using the same instance. I was not. Thus it was not working.
In the end I made my RootViewController a delegate for a method that has the above code. Thus when in another view - this view can call this method and the proper or real instance of RootViewController will implement it.
I am creating a delegate view controller and presenting it to the user to perform an action but I would like to change a NSString on the delegate view controller based on the originating view controller. For example if the delegate view controller is a delegate of viewControllerA, then display Foo, but if its a delegate of viewControllerB then display Blah. ALthough I cant figure out how to pass some sort of information that indicates what the originating view controller is. I noticed that if i do an NSLog(#"I'm from %#",[self delegate]); it will tell me what the originating view controller is, as well as the memory address, but I cant seem to translate that into an NSString object to examine its value. If theres a way to make that work, or a better way to do this then that works too...
- (IBAction)editDate {
DatePickerViewController *datePickerView = [[DatePickerViewController alloc] init];
datePickerView.delegate = self;
datePickerView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:datePickerView animated:YES];
[datePickerView release];
}
It seems like you're using some terminology in ways that are different from what most Objective-C coders would mean.
Here you're instantiating a view controller to show as a modal view. That view controller has a property called delegate that allows it to call some methods to report changes to its state. That doesn't make it a "delegate view controller", that makes it "an object with a delegate".
You happen to be using another view controller class as the delegate, but any object that implements the methods that DatePickerViewController objects want to call to report changes could be assigned to that delegate property.
I think that the question you're asking is "how do I make the DatePickerViewController display different information depending on what kind of object it's reporting to?", and the answer is much the same as "how do I make a UILabel show different text depending on the view controller that created it?"—you set properties or call methods on in when you create it.
If you really just want to pass a string to DatePickerViewController, you could add an NSString* property to DatePickerViewController and set it with arbitrary text, with
datePickerView.myString = #"some information that you want";
You could use the class of the delegate.
if([[self delegate] isKindOfClass:[ViewControllerA class]]) {
[self doViewControllerAThings];
}
else {
...
}