pass value to label inside UITabController from another tab iOS - ios

I have UITabController with labels and buttons inside. I need to pass textView value from one tab to another's label. Using segues.
Simply I need to do like this code:
In TAB1:
textView1 = #"test";
set Tab2.label1 = textView1;
Thanks,
David.

Related

How to load second view ,that can see the first view thourgh it (transparent), in ios 9

This is my question,think that I have two views. in the first view there is a imageview(with a image) and a button.When I press the button then it load second view(with storyboard segue kind is present modally).when that view loads, I want to set the first view, through the second view(should be transparent).
I tried with setting secondview's defatul view background color to default but it gives black.how can I do that.
Oh i fixed you issue after a few minutes test...
I found that segue has a property named presentation,like this:
you may change the property to Over Current Context and it works
Select your secondviewcontroller goto attribute inspector then select Transition style to 'cover vertical' and presentation to 'over current context'
and write the code for presentviewcontroller to present your second view controller modally in firstviewcontroller.
You can add the view of one viewcontroller to another as a subview. This way the code for each viewcontroller is in it's own class, but they are displayed on top of each other.
MyViewController *viewController = [[MyViewController alloc] init];
[viewControllerA.view setFrame:CGRectMake(x, y, width, height)];
[self.view addSubview:viewController.view];

UIViewController Title attribute in Storyboard

I am setting the title field of a UIViewController via Interface Builder/Storyboard:
This view controller is nested in a UINavigationController which in turn is nested within a UITabBarController. When I run the app, I my navigation item has no title, neither does the tab bar item.
If I explicitly set the view controller's navigation item's title, and also it's tab bar item's title in interface builder, then it works just fine.
I am wondering:
a)If I am not using Storyboard but just regular xibs, setting the title of a view controller implicitly sets the navigation items' title as well as the tab bar item's title. But it's not the same storyboard. Is this the intended behaviour?
b) What is then the purpose of the view controller's title (in Storyboard)? it seems to have no effect.
Thanks!
You can set the title of the UINavigationBar in Storyboard by double clicking the actual navigationBar and typing in a title right there. This only sets the title for the UINavigationBar.
Setting the title in code offers some different possibilities.
self.title = #"Your title"; will set the title of a navigationBar and also cause the title to cascade down to a UITabBarItem, if present.
self.navigationItem.title = #"Your title"; will only set the title of the navigationBar, assuming a UINavigationController is present, and NOT affect a UITabBarItem.
self.navigationController.title = #"Your title"; will set the title of a UITabBarItem but NOT the UINavigationBar.
Step 1
If you're looking at a Xib in Xcode's Interface Builder, take a look in the "Document Outline" panel (second panel from the left). Expand the view controller you're working with until you find an icon labelled: Navigation Item.
Step 2
If you then highlight the Navigation Item and open up the Utilities panel (the farthest on the right), and click the Attributes Inspector, you'll see where you can set the title of the view controller. This is the way to do it in Interface Builder, rather than doing it through code.
I ran into this issue this morning. Here are the stabs I took and the final workaround.
This correctly logs the child view controller's title as set in the storyboard, but has no effect on what's being presented:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Title: %#", self.title);
}
This has no effect; the title still doesn't show (probably doing an "if (![_title isEqualToString:title]){}" user the hood:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = [self.title copy];
}
This causes the title to be set correctly:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *title = self.title;
self.title = nil;
self.title = title;
}
My guess is that the UINavigationController is pulling the title for the view being presented before it has been loaded from the storyboard, but then doesn't start listening for changes until after the property has been set. I don't use storyboards or nibs very often, however, so it's quite possible there's a magic checkbox for this hidden somewhere that I've missed.
In any case, it looks like you can either do the self.navigationItem.title = self.title dance, or the above, as a workaround and still maintain your titles in IB.
Apples docs for this are kinda clear:
The navigation controller updates the middle of the navigation bar as
follows:
If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view.
To specify a custom title view, set the titleView property of the view
controller’s navigation item.
If no custom title view is set, the navigation bar displays a label containing the view controller’s default title. The string for this
label is usually obtained from the title property of the view
controller itself. If you want to display a different title than the
one associated with the view controller, set the title property of the
view controller’s navigation item instead.
Emphasis mine.
I just ran into the same problem. I don't understand why it's not working... It might be on purpose or just be a bug.
To change the title in interface builder, you can click on the navigation item directly and change the title there:
Everything else on this page failed. For now, this worked, in code, in viewDidLoad:
NSString* text = #"My page title";
UIFont* font = [UIFont systemFontOfSize:20.0];
const CGSize SIZE = [text sizeWithAttributes:#{NSFontAttributeName:font}];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SIZE.width, SIZE.height)];
label.text = text;
label.textColor = UIColor.blackColor;
self.navigationItem.titleView = label;
If you have UINavigationItem present, then you must use the navigation item title in the storyboard. If you don't have a navigation item for a view controller, then the UINavigationController will use the view controller's title property.
Example :
In your storyboard, if you have a navigation item the navigation controller doesn't need to read the view controller's title. Even if the title field is empty for this navigation item.
Remove the navigation item (if you can, you won't be able to do it for the root view controller but you will for the others) and your title will be correctly loaded
I think it works as designed although we expect another behaviour. If you print the title property in - (void)viewDidLoad it will be the same value that you set in story board so I see no reason of this not working unless Apple's choice.
a) If I am not using Storyboard but just regular xibs, setting the title of a view controller implicitly sets the navigation items' title as well as the tab bar item's title. But it's not the same storyboard. Is this the intended behavior?
I believe this is the intended behavior. I think that the purpose of the title attribute of a view controller is more of a property that can be used at the developer's discretion perhaps for distinguishing between controllers.
Another reason for this change I think is that your navigation item's title may need to be different than the tab bar title, since the tab bar title cannot be nearly as long as the navigation title.
b) What is then the purpose of the view controller's title (in Storyboard)? it seems to have no effect.
I think I mentioned this in my first paragraph. I think that the title attribute of a controller is a property that the developer can use perhaps for distinguishing between controllers.
I tried all of the above methods, even tried manually adding a navigation bar but to no avail.
So this is what worked for me.
Remove any navigation bar item you manually added to the view controller and add this to your viewDidLoad method
self.navigationController.navigationBar.topItem.title = #"My Title";
or
self.navigationController.topViewController.title = #"My Title";
In my case I solve with this:
[self.tabBarController.navigationItem setTitle:#"My Title"];

how do I display different views in a view controller using a tab bar

I have a view controller that has a map view and a second view under the a tab bar. How do I go about updating the second view when I press buttons on the tab bar?
I tried:
LocationNotesViewController lnvc = new LocationNotesViewController();
lnvc.View.Frame = MainPageTabBarView.Frame;
MainPageTabBarView = lnvc.View;
Nothing happens...the view doesn't update.
I want to update the second view with different things when a user clicks on the tabbar...
If you put a UIView underlying whatever data you want displayed in it, you can use the IBAction of the tabBar to programtically cahnge out the contents of the UIView.
Or you could have the IBActions of the tabbar create new UIViews on the fly, containing whatever you want inside.
Code to do this would be like explained here: http://www.programmerscountry.com/creating-uiviewcontrols-programatically/.
Not exactly the same, but you will understand how it works from that answer.
To switch UIViewControllers use this piece of code:
UIViewController *viewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"4"];
[self presentViewController:a animated:YES completion:nil];
I think this is a work around - but I was able to make this happen by doing:
LocationNotesViewController lnvc = new LocationNotesViewController();
lnvc.View.Frame = new RectangleF(MainPageTabBarView.Frame.X, MainPageTabBarView.Frame.Y - MainPageTabBarView.Frame.Y, MainPageTabBarView.Frame.Width, MainPageTabBarView.Frame.Height);
MainPageTabBarView.AddSubview(lnvc.View);

iOS storyboards

I have a iOS single-view application for the iPad and using storyboards.
I have the default viewcontroller created by xcode (vc1). I
place a button on its view.
I then drag a new view controller (screen2VC) onto the
storyboard.
I also create a new view controller subclass (vc2SubClass)
I associate screen2VC to this subclass via the identity inspector.
On screen2VC's view, I have a label. I control-drag the label onto
vc2SubClass to create an outlet.
I create a segue between vc1's button and vc2's button.
In performSegue override i have the following code:
if ([segue.identifier isEqualToString:#"segue1"])
{
screen2VC* vc = [segue destinationViewController];
vc.label1.text = #"screen 2";
}
When I run the code and press the button, the segue works and transitions between the two views just fine, but screen2VC's label is never set to 'screen 2'. As a test, I placed the label1.text = #"screen 2"; code into vc2SubClass, but that made no difference. It seems there's still no association between screen2VC and vc2SubClass, even though the former is subclassing the latter and the label is pointing to a UILabel outlet.
Any ideas?
When you're in performSegue: of vc1, vc2's view hierarchy has not yet been set up. The normal way of doing this is to have a property in vc2 that temporarily is given the string during performSegue:. This property is then used to update the label in vc2's viewWillAppear:.
(When you say "As a test, i placed the 'label1.text = #"screen 2";' code in vc2SubClass but that makes no difference" I'm not sure what's going on there. What method was that code put into?)

How to set first TabBar selected programmatically on iPhone

I have UITabBar in view which have 5 tabs. I am using didSelectItem delegate to open different view i.e. I am NOT using TabBarController.
My problem is on view load I need first tab get selected by default. Is there any property in TabBar which we can set to make it selected?
Thanks.
This code will work [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
In swift if tabbar is used not tabbarcontroller set default select
var tabbar:CustomTabBar?//if declare like this
tabbar!.selectedItem = self.tabbar!.items![0] as? UITabBarItem
or
let tabbar = UITabBar()//if declare and initilize like this
tabbar.selectedItem = self.tabbar.items![0] as? UITabBarItem
set the
tabbar.selectedItem=0; in the viewWillAppear so when ever the view appears it will select the first tab by default.
[self.tabBar setSelectedItem:self.tabBar.items[0]];
or
self.tabBar.selectedItem = self.tabBar.items[0];
The selectedItem property requires a TabBarItem and not an index. So provide the tabbaritem in index 0 for the first tab.
This is wrong then: tabbar.selectedItem=0;
You may select other tabs as well. Happy coding

Resources