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
Related
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/
Basically i am using a segue to display a modal ViewController.
I want to the modal ViewController to display a custom subclass of UIView which i have declared as a strong property in my main ViewController like this:
#property (nonatomic,strong) myView *theView;
in prepareForSegue:sender: i adjust the custom UIViews frame and add it to the modal ViewController:
-(void)prepareForSegue:(UIStoryBoardSegue*)segue sender:(id)sender{
UIViewController *destination = [segue destinationViewController];
[self.theView setFrame:destination.view.bounds];
[destination.view addSubview:self.theView];
}
Aside from the View still being the wrong size, only works the first time i invoke it. The second time it will crash with EXC_BAD_ACCESS on the
[destination.view addSubview:self.theView];
line.
Any ideas why this is happening?
Thanks in advance
I feel ignorant for asking this because I know it's simple. Goal: Retain a variable on a view after leave then returning to it.
For instance: Let's say we have MainView, CategoryView and (drumroll) ProjectView
Application opens to MainView it displays a table - user selects they want to pick a category. This segue's them to CategoryView. Once a selection is made I send the chosen category back to MainView. via
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
No problem here. MainView receives variable and displays the chosen category as subtext in the 'Category' section. Next the user wants to pick a project name (this is also a predefined list) They select the item and I send the variable back the MainView.
-- Now can somebody explain to me (gently) why when I return to the main view the NSString variable that was previously holding the 'chosen' category is now null?
So my NSString selectedProject is not being retained correct? What is the correct implementation I should be doing here? Or what am I missing? I'm really trying to understand whats going on so anything would be a great help.
MainView Interface
#interface MainViewController : UIViewController {
NSString *selectedProjectName;
NSString *selectedCategory;
}
#property (nonatomic, retain) NSString *selectedProjectName;
#property (nonatomic, retain) NSString *selectedCategory;
#end
MainView Implementation
#implementation MainViewController
#synthesize selectedCategory, selectedProjectName;
and if you need it..
ProjectView Implementation
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"masterSegue"]) {
MainViewController *vc = (MainViewController *)[segue destinationViewController];
LogValues *lv = [LogValues alloc];
lv.project = #"Test Project Name";
vc.selectedProjectName = lv.project;
}
}
Ok, your problem is that you're not returning to the same instance. You've missed one very very important thing about segues -- they ALWAYS instantiate new view controllers. So, when you use a modal segue to go "back" to the main view controller, you're just creating a new instance of that controller. You should never go backwards (to earlier controllers) in a storyboard using anything other than an unwind segue. Unwinds are the exception to the rule about segues always creating new instances.
So, you have 2 choices. You can either use an unwind segue, which is nice, because you can still implement prepareForSegue to send data back to the destination controller, or you can not use a segue at all, and use dismissViewControllerAnimated:completion: to go back, and use a delegate to send back the data.
So of course immediately after posting I see a thread in the side that I believe helps. The solution was to store the values in the AppDelegate which is working fine.
I feel however there are other ways to achieve the same result without having to rely on the AppDelegate and would love to hear other solutions.
I am attempting to perform a Storyboard segue from a UIbutton that is a day on a calendar to a DayViewController with info on that day. The currentTitle of the sender UIbutton should be passed to a UILabel in the dayView of the DayViewController. It successfully segues to the new VC and assigns the button title to a property therein, however the dayView (and the base view) of my DayViewController is not getting initialized (their addresses in the debugger are both 0x0) and I get a blank page. How can I get these views to initialize in this segue? (I thought views linked to a VC were automatically initialized when segued to?)
Here's the prepareForSegue getting called:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender
{
if ([segue.identifier isEqualToString:#"ShowDay"]) {
DayViewController *vc = segue.destinationViewController;
[vc setCurrentDayNumber:sender.currentTitle];
}
Here is setCurrentDayNumber in the new VC
-(void)setCurrentDayNumber:(NSString *)currentDayNumber
{
_currentDayNumber = currentDayNumber;
[self.dayView setNeedsDisplay];
}
Here is my view that is linked to DayViewController in the storyboard.
#property (nonatomic, weak) IBOutlet DayView *dayView;
Thanks in advance!
When you declare a property as weak, it will not "hold on" to the value unless someone else has a reference to it as well. When the new view controller is pushed, the old button goes away, leaving currentDayNumber as the only reference, so it automatically sets itself to nil. (This is what weak is supposed to do.)
If you want to keep a reference to it no matter what, use strong instead of weak and make sure to set _currentDayNumber to nil somewhere (like viewDidUnload) so that you don't retain it forever.
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).