in my application I have a UIView object in second view controller. Now I want to display the UIView as a popup or sub view when I click the button from first view controller. Please tell me how to do this? I have seen many solution for the nib files but I didn't find any for storyboard.
I have connected the IBOulet of view in my second view controller.
#property (strong, nonatomic) IBOutlet UIView *popview;
And i have imported the second view controller in my firstview please tell me how to make this one i have been stuck here for long time please help me out on this one.
Thanks.
In your FirstViewController which has UIView *popview:
- (void)viewDidLoad
{
[super viewDidLoad];
// you don't want to show PopView
self.popview.alpha = 0;
}
- (void) showPopView
{
//you want to show PopView
self.popview.alpha = 1;
}
Try Adding this in your code
-(void)viewDidLoad
{
popView.hidden=true;
}
and when ever user clicks an action change the hidden property to false just like
-(IBAction)AnyActionPerferformed:(id)sender
{
popView.hidden=false;
}
You can Learn about this from
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html
You don't need an extra UIViewController
In your storyboard, drag a new UIView to your view controller. (It will be your popup view)
Link to your view controller. (#property (strong, nonatomic) IBOutlet UIView *popview;)
When you want to hide popview set it's alpha to 0.
Related
I am relatively new to Xcode and have tried to find the answer by searching, without luck.
My app has 5 View Controllers, V1 through V5, which are embedded in one Tab Bar Controller. Each View Controller has a segue to one and the same Setup Menu View Controller. The Menu changes some labels on the View Controllers. I use a delegate to make sure that the View Controller that calls the Menu gets updated with the new settings when you leave the Menu. However, this allows me to modify only the labels on the View Controller that called the Menu Controller, not on the 4 other ones.
I work form a Story Board. Is there a simple way to set the UILabels on V2, V3, V4 and V5 from V1 (and vice versa), or even better, set the labels on V1 through V5 from the Menu View Controller (which is not embedded in the Tab Bar Controller)?
I have seen something that could help here, but this seems rather complicated for what I want. The label changes I need are quite simple and are all predefined. Is there a method that is called every time you switch tabs in a tabbed application? Similar to ViewDidLoad?
This sounds like a good time for NSNotificationCenter. You are going to have your MenuViewController generate a notification with the new data that should be updated in your other view controllers:
// User has updated Menu values
[[NSNotificationCenter defaultCenter] postNotificationName:#"MenuDataDidChangeStuffForLabels" object:self userInfo:#{#"newLabelValue" : labelText}];
In your V1, V2, etc. you can add subscribe to these notifications using this code in your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
// Subscribe to NSNotifications named "MenuDataDidChangeStuffForLabels"
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateLabelText) name:#"MenuDataDidChangeStuffForLabels" object:nil];
}
Any object that subscribes using that code will call the updateLabelText method anytime a notification with that name is posted by the MenuViewController. From that method you can get the new label value and assign it to your label.
- (void)updateLabelText:(NSNotification *)notification {
NSString *newText = notification.userInfo[#"newLabelValue"];
myLabel.text = newText;
}
What I would do is subclass the tab bar controller and set that as the delegate for the menu view controller. From there, you can get updated when the labels are supposed to change and then communicate with the 5 tabs and update the labels.
Alternatively, you could use NSNotifications to let all the 5 view controllers know when settings change.
Lastly, you could add the menu settings to a singleton and have all of the view controllers observe the various properties that can change.
The label changes I need are quite simple and are all predefined. Is there a method that is called every time you switch tabs in a tabbed application? Similar to ViewDidLoad?
Regarding this question, the methods you're looking for are viewWillAppear: and viewDidAppear.
Here is a very simple solution if your workflow is also simple. This method changes all the labels from the different ViewControllers directly from what you call the Menu ViewController.
Let's say you have the following situation :
The blue ViewController is of the FirstViewController class. The green ViewController is of the SecondViewController class. The labels on each of those are referenced by the properties firstVCLabel and secondVCLabel (on the appropriate class' header file). Both these ViewControllers have a "Modal" button which simply segues modally on touch up inside.
So when you clic on any of these two buttons, the orange ViewController (of ModalViewController class) is presented. This ViewController has two buttons, "Change Label" and "Back", which are linked to touch up inside IBActions called changeLabel: and back:.
Here is the code for the ModalViewController :
#import "ModalViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#interface ModalViewController ()
#end
#implementation ModalViewController
// Action linked to the "Change Label" button
- (IBAction)changeLabel:(id)sender {
// Access the presenting ViewController, which is directly the TabBarController in this particular case
// The cast is simply to get rid of the warning
UITabBarController *tabBarController = (UITabBarController*)self.presentingViewController;
// Go through all the ViewControllers presented by the TabBarController
for (UIViewController *viewController in tabBarController.viewControllers) {
// You can handle each ViewController separately by looking at its class
if ([viewController isKindOfClass:[FirstViewController class]]) {
// Cast the ViewController to access its properties
FirstViewController *firstVC = (FirstViewController*)viewController;
// Update the label
firstVC.firstVCLabel.text = #"Updated first VC label from Modal";
} else if ([viewController isKindOfClass:[SecondViewController class]]) {
SecondViewController *secondVC = (SecondViewController*)viewController;
secondVC.secondVCLabel.text = #"Updated second VC label from Modal";
}
}
}
// Action linked to the "Back" button
- (IBAction)back:(id)sender {
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
For the sake of completeness, here are FirstViewController.h :
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *firstVCLabel;
#end
And SecondViewController.h :
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *secondVCLabel;
#end
There is no relevant code in the implementation of these classes.
Thanks a lot guys, I am impressed by your quick responses. In this particular case, viewWillAppear does the trick:
- (void)viewWillAppear:(BOOL)animated
{ [self AdaptLabels];
NSLog(#"View will appear.");
}
Every time a new tab is chosen, it updates the labels in the new View, according to a global variable set by the Menu, just before they appear. Very quick and clean. Thanks to all of you!
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
i have two UIViewController class with xib from interface builder, and i want create another UIViewController with xib that contains this two view, it's possible and how i can achieve this feature?
You can't add View controller A and B's views to view controller C in an XIB because there is a fair amount of housekeeping code you need to do to set up the parent/child view controller relationship.
However, if you were to use storyboards, and you are running in iOS 6 or later, this would be trivial. There is a new "container" view in iOS 6. You just add a container view to your scene, and then control-drag from the container view onto another view controller's scene, and IB sets up an embed segue that does all the housekeeping for you. In my view this feature alone is enough to justify moving to storyboards.
You add two UIView in your third UIViewController xib and you link these UIView with IBOulet.
#property (nonatomic, weak) IBOutlet UIView *firstView;
#property (nonatomic, weak) IBOutlet UIView *secondView;
After that you add your UIViewController in theses subviews in the viewDidLoad for example :
- (void)viewDidLoad
{
[super viewDidLoad];
MyFirstViewController *myFirstViewController = [[MyFirstViewController alloc] initWitNibName:#"MyFirstViewController"];
MySecondViewController *mySecondViewController = [[MySecondViewController alloc] initWitNibName:#"MySecondViewController"];
[self.firstView addSubview:myFirstViewController.view];
[self.secondView addSubview:mySecondViewController.view];
}
Improve it with #property of First and Second ViewController and Lazy loading.
Does UIScrollView only work on a view controller that is embedded in a Navigation Control? The reason I ask is because I have a UIViewController that has a UISCrollView to show all the content that exceeds beyond the view. This ViewController is displayed from a push segue from a TableViewController, which is embedded into a Navigation Controller.
Everything worked fine until I tried to change the push segue to a modal seque instead, which thus removed the view from being embedded into a Navigation Controller.
All the content loads just fine but it won't scroll. I have added an outlet and tried programmatically setting the contentSize and scrollEnabled properties but still nothing. It won't scroll any more and I can't figure it out. I have been searching on the net but I can't find an answer.
Outlet:
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
Implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = CGSizeMake(320, 900);
self.scrollView.scrollEnabled = YES;
}
There are a number of posts indicating issues with setting the contentSize of a scrollView in viewDidLoad. Try moving it to viewDidAppear.
I had to put the UIScrollView in another empty UIView in order to get it to work.
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated
{
self.scrollView.contentSize = CGSizeMake(320, 900);
self.scrollView.scrollEnabled = YES;
}
I set up my app using a storyboard and have my main view controller embedded in a UINavigationControler. To change the title that appears in the navigation bar, in the viewDidLoad method of my main view controller, I have self.navigationItem.title = #"My Title"; But the title never gets set.
My guess is that I need to set up a Reference Outlet in my storyboard, but I'm not sure what needs to be connected to what. Any ideas?
in story board, it doesn't get automatically connected , make a UINavigationItem using the following code in ur .h file
#property(weak, nonatomic) IBOutlet UINavigationItem *navBar;
in .m file synthesize the property and set the title like this
#synthesize navBar;
-(void)viewWillAppear:(BOOL)animated {
[self.navBar setTitle:#"Sign In"];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
Also, don't forget to connect the UINavigationItem "navBar" in the storyBoard with ur class so that storyboard knows whose title to change. In case u don't have a UINavigationItem in ur storyboard,add it outside the UIView and then connect it properly
Just setting the title property of the view controller you are in should do the trick.
[self setTitle:#"Best Page Ever"];
After you have allocated your rootview..use:-
YourRootView *rootView=[YourRootView alloc]init];
rootView.title=#"yourTitle";
and then initialize it in uinavigationcontroller.(I don't have idea if you have your view on storyboard , then set its title just after allocating and then pass it as parameter to navigationcontroller).