add searchbar in UItoolbar - iphone

i want to know is this possible to add a searchbar in UItoolbar

I just had a quick look and while interface builder will let you do it, when you try to save a warning pops up a warning saying the following "UISearchBars are not supported in UIBarButtonItems in non-iPad documents". That being said, it might be possible to make your own view that looks and works like a UIToolbar that does allow a UISearchBar.

You can nest:
navItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView: m_searchBar] autorelease];

If you get an error stating:
UISearchBars are not supported in UIBarButtonItems in non-iPad documents
adding a UISearchBar to a view within a UIBarButtonItem worked for me.

Related

How can I hide a UIBarButtonItem?

I have created a simple UI in IB, this consists of a UINavigationBar and a UIBarButtonItem that I dragged and dropped on the right hand side.
I am trying to set this button to be hidden a certain times but I am having some problems.
So far I have tried using:
self.NavigationItem.rightBarButton = nil;
...which didn't work for me. I have also tried creating and IBOutlet and linking it to the button however I'm having problems with this too. I think it should be pretty simple and maybe I'm over-complicating it, but at this point I'm pretty stumped!
Please can someone help me out?
UINavigationItem doesnt have a rightBarButton property. Try rightBarButtonItem instead (or [self.navigationItem setRightBarButtonItem:nil animated:NO];):
self.navigationController.navigationItem.rightBarButtonItem = nil;
// Or
self.navigationItem.rightBarButtonItem = nil;
// Or
[self.navigationItem setRightBarButtonItem:nil animated:NO];
Just reset the buttons
-(void)setItems:(NSArray *)items animated:(BOOL)animated
More info here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/occ/instm/UIToolbar/setItems%3aanimated%3a
You can get the current items using the items property, then just remove the one you don't want to show and pass in the new NSArray.
You can also add a UIButton as the UIBarButtonItem's customView. Then set the hidden property on the customView (UIButton)
Rather than deleting the bar button item and destroying the button and it's attached storyboard segue, you can just set it to clear text when it's disabled.
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor clearColor]}
forState:UIControlStateDisabled];
Then when ever you want the bar button item hidden, you can just do:
self.navigationItem.rightBarButton.enabled = NO;
It's lame there's no hidden property but this offers the same result.
Actually, you can just create an IBOutlet reference to the desired UIBarButtonItem and when needed just do as follow:
[self.yourOutletRerence setImage: nil];
The simplest solution: Just change the BarButtonItem's identifier to custom.

Overwriting UINavigationBar to set a default titleView

What I want to do is a navigation bar with a image on it. I have a tab controller on my main view, and inside each tab I have a UINavigationController. From inside the UIViewController that my tab/navigationController calls, I could set the titleView without much problem, doing this inside the viewDidLoad method:
self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mylogo.png"]] autorelease];
But, I want to replace all titles in my navigationBar for this view, and it seems ugly to repeat this everywhere. So I did this on the delegate (after linking all the Outlet stuff)
self.tabOneNavController.navigationBar.topItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mylogo.png"]] autorelease];
Again, it worked! ok, I'm almost getting there.
But the point is, I've 5 tabs and all of them have navigationControllers inside. I reduced the code repetition from every internal view to only 5 times, but it still. It requires that I do that for the NavController of each tab.
Then I tried to extend the UINavigationBar to create my own, where I could set this in the initializer, and use it in the interface builder as the object class. But it doesn't seem to work. Here is what I did:
#implementation MyNavigationBar
- (id)init {
self = [super self];
self.tintColor = [UIColor greenColor];
self.topItem.title = #"testing please work";
return self;
}
#end
in the interface file MyNavigationBar inherits from UINavigationBar. But this didn't work. Should I overwrite other method? which one? is this a good practice?
I'm not even sure if I should add one navigationBar for each tab, as I said, I have tabs and I want to have a navigation bar / navigate inside them. By now, after a near death experience trying to figure out how the interface builder / outlets and classes work, the code is working, I just would like to make unglify it.
Thank you!
The problem of repeating code which you describe has an elegant solution. Objective-C supports something called a "category", which allows you to add methods to a class. A common use for this is to customize navigation and tab bars. In Xcode 4, you would do something like this to add a category on UINavigationBar:
Hit Command+N or open the "New File" dialog. Next, choose "Objective-C category" from the Cocoa Touch menu:
Click Next and you will be prompted to enter the name of the class that you would like to add methods to as a category. It should look something like this:
Then, you should end up with a save file dialog. A quick note about convention here. Convention is to name a category after the original class, the plus sign, and then a description of what you're adding. Here's what yours might look like:
Once you save your file, you will need get something like this:
Look at that beauty. You can now override the default drawing/init methods as well as extend the functionality of the navbar.
I'd suggest looking into the init and drawRect methods, although I don't remember which ones people use. Also, please note that while under NDA, this may change in iOS 5, so just be prepared for that possibility.
Why not define a UIViewController subclass which sets the title view via self.navigationItem.titleView and have your other view controllers extend from that class? Then you're sharing that behavior across all of your controllers without repeating the implementation.

Custom UIAlertView?

Seeing as the blue doesn't go with my UI interface, im just wondering if there is a way to change the colour of the uialertview, or use a image instead. With all the buttons, 'dismiss' etc still there
Thanks
The fine folks at CodeCropper just put out an open-source control that lets you create custom alert views. It's awesome.
https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets
You could try presenting a Modal View Controller with a transparent background.
ModalViewController *popupController = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[self presentModalViewController:popupController animated:NO];
Something like this for the ModalView (http://stackoverflow.com/questions/849458/transparent-modal-view-on-navigation-controller)
This way you can create a custom Alert, but it's really a modal view that you can customize
You can either go through its subviews and change what you need to change, or subclass it. Because UIAlertView inherits from UIView you can use:
myAlertView.subViews
and modify the views or subclass UIAlertView to create your custom AlertView. Here is a very good article on how to sublass UIAlertView to get whatever design/color you want.
Subclassing UIAlertView
Basically what you want to override is this method:
- (void) drawRect:(CGRect)rect
Hope that helps.
you can use a uiview instead of uialertview and can easily customize uiview according to your needs
You could use CODialog. It's fully style-able and configurable.
Subclassing UIAlertView is not an option, this class is not intended to be subclassed and doing so might be a reason of app rejection.
Instead, you might try to go through all alert view's subviews or create your own class
In case you are going to create your own class, here's an example of how to fake UIAlertView:
http://iosdevtricks.blogspot.com/2013/04/creating-custom-alert-view-for-iphone.html
If looking for custom alert view then it might help.
https://github.com/Pradeepkn/PKCustomAlertView/
Hope it helps some one.
No need of setting delegate. You will get call back once the action completes on same method.
Enjoy :)
Custom Alert view
Table Alert view

How can I set the background image of multiple buttons in a certain view from a different view?

Basically, I cannot figure out how to change the background image. I have searched and searched and just cannot seem to find it. Anyone have any ideas? Thanks!
Update
Here is the code I use to show the View:
SettingsViewController *settingsView = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:settingsView animated:YES];
If anyone needs anything else to help out I'll do my best! Thanks!
Passing a messages between different views can be done by direct call of the methods (not a good architectural solution but maybe not critical for a small projects) or with an event driven model as described in an answer above.
As for binding of a collection of objects and processing the afterward I recommend to review IBOutletCollection keyword that allows to bind multiple objects fro an InterfaceBuilder to a property with a type like NSArray.
Property declaration will look like following:
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;
The code to change the background for all the buttons will look like following:
UIImage *backgroundImage = [UIImage imageNamed:#"background.png"];
for(UIButton *button in buttons) {
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
}
You use setBackgroundImage:forState: to set the image.
Regarding the different views part, it depends on how you wrote your code. If the view with the buttons is controlled by the different view (by creating an instance and using addSubview:) then you can call it directly by using instanceName.buttonName (as long as you declare it as a property -thanks fichek).
If you don't manually add the view, instead through IB, you can have the button that controls the other button's image point to the IBAction in that class.
If neither of those options work you can always use NSNotificationCenter.

Add UIBarButtonItem in interface builder, how to?

I have a navigation based application that have a uinavigataioncontroller containing uitableviewcontrollers. I want to add buttons to the UINavigationbar of the uinavigataioncontroller, usually I write code to add these buttons, something like this:
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(saveClicked:)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
My idea now was to use interface builder instead. But I'm not sure how to do it. What I'm trying to do is to add a UINavigationItem in the xib file of the viewcontroller (my viewcontroller is called TextViewController), something like this:
See this image: http://i48.tinypic.com/qq5yk7.jpg
But how can I make TextViewController use the UINavigationItem I added? The button I add doesn't show in the navigationbar.
Any ideas on how to do this? What am I missing?
You can.
All you have to do is drag UIBarButtonItem from Object Library to corresponding NavigationBar of your Xib.
After that then you can connect it to become an IBOutlet of your class.
You can take a bar button in the coding like IBOutlet UIBarButtonItem * saveButton;
Then you have to take a bar button in the xib file and bind this button with that and in coding you can directly write
self.navigationItem.rightBarButtonItem = saveButton;
Its almost the same as your code but the only thing changes is you dont have to initialize the barbutton..
Happy coding...