ios:How to persist object value in viewcontroller - ios

I am having one view controller WebViewController and second is Settings. On tap of button i am adding Settings view controller as subview of webviewcontroller.
menusettings=[self.storyboard instantiateViewControllerWithIdentifier:#"SET"];
[menusettings.view setFrame:CGRectMake(0, 0, 700,600)];
[self.view addSubview:menusettings.view];
I have Webview Object in the
Webview controller
#property (strong, nonatomic) IBOutlet UIWebView *wrae_one;
and i am loading data in webview but when i cal function from Settings controller then the value self.wrae_one becoming null. Means when i add setting view controller as subview in webview controller then obejet of webview become null. Is there any way to main it value. In settings i am using this code for calling the function
self.home_webview=[[PRGVwebViewController alloc]init];
[self.home_webview loadWebviewController]

I think that PRGVwebViewController is the class from which the settingsviewcontroller is instantiated. So why do you re alloc the class? Doing this you are creating a new controller, but when you dismiss the settingsviewcontroller, you are seeing the first one.
You have to create a reference to the previous controller.
Maybe, the best way is to instantiate the WebViewController as a sharedIstance (singleton), so you can access to it (and only it) wherever you want.
This can be helpful: http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/

Related

Why is it null when I try to access data in another view controller? (Objective-C, XCode)

I am trying to use the .text of UITextField in my first view controller in another .text of UITextField in my second view controller, but my firstPage.firstTField.text turns out to be (null) in my second view controller even though I printed _firstTField.text in my first view controller and it printed out the input that was entered.
What may be the problem? Why is null?
FirstViewController.h
#property (weak, nonatomic) IBOutlet UITextField *firstTField;
SecondViewController.h
#property (weak, nonatomic) IBOutlet UITextField *secondTField;
SecondViewController.m
#import "FirstViewController.h"
- (void)viewDidLoad {
[super viewDidLoad];
FirstViewController *firstPage = [[FirstViewController alloc] init];
_secondTField.text = firstPage.firstTField.text;
}
You should treat a view controller's views as private. That's part of the appearance of the view controller, not it's function, and if objects outside of the view controller expect the views to look a certain way then when you change the appearance of the view controller's views it breaks other code. Bad.
In this situation it's worse. It just doesn't work, for the reason #nhgrif explains. You just created a new FirstViewController object, and it's views don't even exist yet. (A view controllers views are not created until the system is asked to display the view controller to the screen.)
You should create a property in your view controller that exposes the string(s) you need to read/write and use that instead.
However it's strange that you would create a new instance of a view controller and then immediately try to read text from one of it's fields. How can it possibly have useful data if the view controller was created on the line before? What are you expecting to happen?

Passing information from one UiView to Another

I am beginning to learn Objective-c and my problem is the following:
I have created a UIView to create a profile and in that UIView, i have an imageView for the Picture as a Datepicker for the Birthday and a Slider for the Weight of the person. When the user presses the button "Conclude" i wish for him to have his Profile created and show all the info that it was set before , Picture , Birthday,name and Weight there.
I tried to use prepareForSegue but i have no idea of what i am doing so could you guys give me a little hint on what i can do to make it work? thank you :)
In the view controller you will be segueing to, you can setup variables to hold the information from the original view controller, then assign them in prepare for segue.
So in your destination view controller .h file, declare
#property (nonatomic) UIImageView *imageView; then in your prepare for segue in your original viewController
NextViewcontroller *nvc = [segue destinationViewController];
nvc.imageView = currentImageView;
You can access the imageView in the destination view controller by calling self.imageView

Passing Data to subView Using same .h and .m class in iOS

I have aded a new View in my app, its just a .xib file
On button click i am calling this new view... AS
UIViewController *newView = [[UIViewController alloc] initWithNibName:#"Welcome" bundle: [NSBundle mainBundle]];
[self.view addSubview:newView.view];
It call successfully the new view.
I have set some labels and button newView that are shown on button click.
now the problem is this,
1- i have to send or pass some data to newView from main and also
2- when i connect a UILabel using CONTROL Drag in interface builder on selecting File's Owner Of newView with desired Label it gives me error on button click
'[<UIViewController 0x89d05b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Users.'
'User' is the UILabel type variable In my .h file that is declared as
#property(nonatomic ,retain) IBOutlet UILabel *Users;
any suggestion or help would be appriciated
You're creating a UIViewController instead of your custom UIViewController subclass. What you should be using is:
MyViewController* vc = [[MyViewController alloc] initWithNibFile:...]
Also note that if you're using child view controllers, you should be calling addChildViewController with the newly created view controller so that the UIViewController methods are properly propagated to the child view controller.
One last note, none of this will work reliably before iOS 5, as up to that point child view controllers were strongly advised against.
It also sounds like you're not using a subclass for the child view controller, if you add properties or actions, you have to use a subclass.

In controller A, open contained controller B through UIButton

How do I hook up a UIButton that is inside controller A, so that it opens a controller B that is contained inside controller A (inside a "Container View") using storyboards?
Ie controller B only takes up part of controller A area. Controller A would still be partly visible.
Background:
When adding a controller B to a Container View inside another controller A, it defaults to opening the controller B as soon as controller A loads. I want controller B to be hidden first, then have it open by the tap of a button.
Taking apart the view lifecycle for nib/storyboard launched resources will help here.
You need to hide the view of Controller B sometime after it has been created and loaded, but before it has been displayed. Then in response to an action, you need to unhide the view (or do some fancier presentation).
Typically you will declare a property within Controller A of:
#property (weak, nonatomic) IBOutlet ControllerB *controllerB;
Which you wire up in the storyboard.
Now you have a reference to to your controllerB instance which you can make use of from within controllerA's code.
Since you've nested controllerB's view inside of the view hierarchy of controllerA in the storyboard, your instance of controllerB will exist and be ready to manipulate as soon as -viewDidLoad is called on controllerA.
- (void)viewDidLoad
{
[_controllerB.view setHidden:YES];
//other setup and configuration of controllerA
}
You could do this at viewWillAppear, or a few other places, but as long as you hide controllerB.view before -viewDidAppear is called, you'll be fine.
Then you have controllerA respond to the button push something like this:
- (IBAction)userPressedTheButton:(id)sender
{
[_controllerB.view setHidden:NO];
}
This is a pretty easy stuff. You could create an outlet for the container view
#property (weak, nonatomic) IBOutlet UIView *containerView;
In viewDidLoad just hide it
- (void)viewDidLoad
{
[super viewDidLoad];
self.containerView.hidden = YES;
// Do any additional setup after loading the view, typically from a nib.
}
Unhide it on button click

Update IBOutlet on Main View from Sub View

I have a main view which has a UISlider on it.
From the main view I add a subview using:
gameView *myViewController = [[gameView alloc] initWithNibName:#"gameView" bundle:nil];
[self.view addSubview:myViewController.view];
The subview is created on top of the main view.
When I remove the sub view using:
[self.view removeFromSuperview];
the main view underneath becomes visible.
I want to be able to update the value of the UISlider on the main view, from the sub view, before I call [self.view removeFromSuperview]
Is it possible?
Basically the question can be generalized to how to update an IBOutlet on the main view from the sub view.
Help is greatly appreciated.
Many thanks!
Yes, it's possible.
And there's a few ways to do this. Here's how I would do it:
First, make your parent view controller's UISlider a property that can be accessed by other objects.
Secondly, give your gameView object an instance variable that you'll link to the parent view (let's call it id savedParent;)
Then, before you do removeFromSuperview, you can simply do something like:
ParentViewController * parentVC = (ParentViewController *) savedParent;
if(parentVC)
{
// some float value of whatever you want to set the slider value to
parentVC.slider.value = 0.5f;
}
Also, why are you instantiating a whole View Controller object (gameView) if you simply want to add a subview? When you do your removeFromSubview call, the view gets removed but your gameView view controller isn't released (and might even be getting lost & leaked in memory, leading to a crash). If you want to do a subview, subclass UIView. If you want to push a new view controller, push the whole controller (and not just the view it contains).
Here is another way:
I'm not sure what the slider is representing, but you need to create an object that represents this
#interface MyGameThing : NSObject
#property (assign) CGFloat myValue;
#end
#implementation MyGameThing {
CGFloat *_value;
}
#synthesize myValue = _myValue;
#end
You then need to pass that object to both of your view controllers (or make it a singleton).
Then, on ParentViewController, in the viewWillAppear, just set the slider to the new value.
Daniel.
(p.s. don't just add view controllers views to the superview, use presentModalViewController / dismissModalViewController or a navigation controller).

Resources