Changing the Title View of a UINavigationController UIViewController in Interface Builder - iphone

Is it possible to change the title view (i.e. add a UISegmentedControl) for a UIViewController contained within a UINavigationController from within Interface Builder? I am currently doing this work from within code with:
-(void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.titleView = self.filterSegmentedControl;
}
However, I'd prefer to set everyone up in IB. Thanks!

Using IB to add a UISegmentedControl to a NavigationBar

Related

How UIActivityController is presented?

I am trying to present a custom size UIViewController on top of UITableViewController without dismissing the UITableViewController but I could not. All I could do was to add the view of the UIViewController as subview.
The UIActivityController seems to be working differently when it is presented. The presenting view controller is not set to nil.
How can I present a custom size UIViewController -as the UIActivityController- without dismissing the presenting view controller ?
Thank you
Here's something you can do:
ActivityViewController *activity = [[ActivityViewController alloc]init];
[self addChildViewController:activity];
[self.view addSubview:activity.view];
Here, ActivityViewController is your custom UIViewController and inside its viewDidLoad or so, you can set its frame to the height you like. Or you can have a method which can be used to vary the height according to the number of buttons you have inside that UIView

How to make UIViewController's view inherit from another UIView?

I have one UIViewController without NIB file. Now i have one my customized UIView. I want to make UIViewController's view inherit from my customized UIView, is it possible ? I know that if I have XIB file than I can make Custom Class from there but without XIB can it be done?
Thanks in Advance
If you are using a UIViewController subclass, and don't want to use a nib file, then override the loadView method in your subclass:
#implementation MyViewController
- (void)loadView {
self.view = [[MyView alloc] init];
// additional view setup here
}
If you are just using a generic UIViewController (not a subclass), then you might be able to just assign to the view controller's view property, like this:
vc.view = [[MyView alloc] init];
But I'm not sure if that works properly outside of loadView. I haven't seen any documentation that says it is or is not allowed.
Replace your view controller's view like this:
// ViewController.m, in viewDidLoad
self.view = [[MyCustomView alloc] initWithFrame:self.view.bounds];
A more conventional alternative is to fill the default view with the custom subview, like this:
MyCustomView *myCustomView = [[MyCustomView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:myCustomView];
I think you need to load a custom view programmatically, for that you need to override the loadView method of your view controller.
-(void)loadView
{
[super loadView];
CustomView *view = [[CustomView alloc]initWithFrame:self.view.bouds];
[self.view addSubview:view];
}
Replace this in your .h file
#interface ViewController : UIViewController
with
#interface ViewController : Customized View Controller

setFinishedSelectedImage:withFinishedUnselectedImage: + Storyboards

I have my interface completely laid out with Storyboards (including the UITabBarController and all corresponding views).
Now comes the time to customize the Tab Bar. Since I have icons that are already set to the correct colour, I can't use [[UITabBar appearance] setTintColor:] (it just keeps looking wrong).
Turns out I'm supposed to use setFinishedSelectedImage:withFinishedUnselectedImage: on the specific UITabBarItem's.
Is it possible to use this method from the AppDelegate (where the rest of my global customization occurs)? How does the AppDelegate know which UITabBar to target?
If instead, I'm supposed to customize each UITabBarItem from each UIViewController, how do I reference the UITabBar (or "root view controller"?) and then specific item from the UIViewController?
Any help would be greatly appreciated.
Thanks!
In viewDidLoad of your UIViewController instances, you can do
[self.tabBarItem setFinishedSelectedImage: withFinishedUnselectedImage:]
Try this
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"t1s"] withFinishedUnselectedImage:[UIImage imageNamed:#"t1"]];
[self.tabBarItem setTitle:#"Title"];
}
return self;
}
Also, delete image in tabbar item from storyboard view

iOS4 - custom uiview accessing UINavigation stack

Bit confused with this one so bear with me...
I have a Navigation-based project which is working fine. I'm trying to create my first custom UIView to make a couple of buttons which I will use in multiple places. One of the buttons needs to push a viewcontroller into the navigation when it's clicked but I'm not sure how to do this.
When I had the button set up within a view controller I was using:
LocationViewController *controller = [[LocationViewController alloc] initWithNibName:#"LocationViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
but the self.navigation controller won't work now, will it? How do I access the navigation controller of the viewcontroller that this uiview will be added to?
Hope at least some of that makes sense, as I said it's my first go at subclassing the uiview and adding it to multiple pages so I'm a bit lost.
EDIT TO ADD - I have the button click events inside the custom UIView, so that is where I'm trying to change the viewcontroller from. Should I instead wire up the events in whichever viewcontroller I add the view to?
Usually your appDelegate has a UINavigationController property. You can access it in your custom view like this:
UINavigationController *navController = (MyAppDelegate *)[[[UIApplication sharedApplication]
delegate] navigationController];
But more effective way is to make delegate method for your custom view and handle button action in your viewController.
MyCustomView.h
#protocol MyCustomViewDelegate
#interface MyCustomView : UIView {
id<MyCustomViewDelegate> cvDelegate; }
#property(nonatomic, assign) id<MyCustomViewDelegate> cvDelegate;
#protocol MyCustomViewDelegate #optional
-(void)didClickInCustomView:(MyCustomViewDelegate*)view withData:(NSObject*)data;
#end
MyCustomView.m
- (void)myButtonClick:(id)sender
{
[self.cvDelegate didClickInCustomView:self withData:someData];
}
So now you can handle this event in any place where is your custom
view.
Add the button from the interface builder or from the view controller's viewDidLoad using code:
CGRect frame = CGRectMake(0, 0, 24, 24);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button addTarget:self action:#selector(handleMyButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Then implement -(void)handleMyButton:(id)sender {}; in your view controller. Or you could instead write -(IBAction)handleMyButton:(id)sender {}; and link method and button using the interface builder.
Then inside the method just paste the block of code you posted above. If you started with the Xcode navigation controller template project it should work.
I think it's cleaner to hide the designated initializer initWithNibName: because it is an implementation detail.
When you say you are subclassing the UIView I don't know exactly what you mean. If you want to add another view controller with a custom view just use the UIViewController template and customize the XIB file, no need to subclass an UIView unless you are really modifying its behaviour, which I guess you are not. The view is a view, and the controller stuff like handling buttons should be in the controller.
The actual controller need to be in the navigation controller stack to be able to push another controller.
Or you can make a new navigation controller instance and push your LocationViewController.

Where to set UITableview style to grouped style

My Tabview controller has a tableview appearing as the first and initial view. The view controller class is a subclass of uitableviewcontroller, so I am not using XIB for this.
Since I am giving the view name directly in the interface builder for the MainWindow of the tab view, I am not sure where I can set the style of the tableview, which appears in 'plain' style at the moment. I want to change it to 'grouped', however there is no code where I call and allocate the tableview controller (in which case I would have used the initWithStyle).
Any ideas on how I can change this initial tableview to grouped style instead of plain style?
I also tried overriding the initWithStyle and set it like below, but does not work.
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Custom initialization.
}
return self;
}
Thanks,
Try the following then:
UITableView* table = [[UITableView alloc]initWithFrame:myFrame style:UITableViewStyleGrouped];
Replace UITableView by the name of your custom class
you can choose the tableview from the .xib and set the style to Grouped