load a xib file as a UIView, and I got a UITextField form a Xib file(it just a view that contains textfield and button).
But when the app launched and that textfield comes up. i cannot type any alphabet on this UITextField.
But paste a text to that UITextField was OK. Why?
Here is my adding code
-(IBAction)showSheet{
UIView *myView = [[[NSBundle mainBundle] loadNibNamed:#"LoginViewComp" owner:self options:NULL] lastObject];
[myView setFrame:CGRectMake(0, 44, 320, 400)];
UIButton *btn1 = (UIButton *)[myView viewWithTag:101];
UITextField *field = (UITextField *)[myView viewWithTag:102];
UILabel *lbl = (UILabel *)[myView viewWithTag:103];
[field setEnabled:YES];
[btn1 addTarget:self action:#selector(testBtn1) forControlEvents:UIControlEventTouchUpInside];
DTActionSheet *sheet = [[DTActionSheet alloc] initWithContentView:myView sheetTitle:#"Demo"];
[sheet showInView:self.view];
[sheet setUserInteractionEnabled:YES];
}
-(void)testBtn1{
NSLog(#"btn1 touch up inside");
}
#end
DTActionSheet.m
#interface DTActionSheet()
#property (nonatomic,strong) UIView* contentView;
#property (nonatomic,strong) UIToolbar* toolBar;
#end
#implementation DTActionSheet
#synthesize contentView=_contentView;
#synthesize toolBar=_toolBar;
-(id)initWithContentView:(UIView*)contentView sheetTitle:(NSString*)title;
{
self = [super init];
if (self)
{
_contentView = contentView;
int btnnum = (contentView.frame.size.height-5)/50; // (height+44-20-25)/50
for(int i=0; i<btnnum; i++)
{
[self addButtonWithTitle:#" "];
}
_toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *titleButton = [[UIBarButtonItem alloc] initWithTitle:title
style:UIBarButtonItemStylePlain
target:nil
action:nil];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone
target:self
action:#selector(done)];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cancel)];
UIBarButtonItem *fixedButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[_toolBar setItems: [NSArray arrayWithObjects:leftButton,fixedButton,titleButton,fixedButton,rightButton,nil]];
[self addSubview:_toolBar];
[self addSubview:_contentView];
}
return self;
}
-(void)done
{
[self dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancel
{
[self dismissWithClickedButtonIndex:0 animated:YES];
}
#end
Hey, guys, have any idea?
try using UIKeyInput Protocol Reference for input --
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIKeyInput_Protocol/Reference/Reference.html
make sure to use its delegate..
Related
I would like to create a NavigationBar with images as buttons on the right side of the NavigationBar.
Something like below Snapshot
How can I achieve this?
Hope This Helps
viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage2.jpg"]]];
viewController.navigationItem.rightBarButtonItem = item;
Here the Code, Just call below Methods From viewDidLoad method
- (void)addCustomButtonOnNavBar
{
UIBarButtonItem * item1= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"passImageNmae1"] style:UIBarButtonItemStylePlain target:self action:#selector(yourButtonAction1)];
UIBarButtonItem * item2= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"passImageNmae2"] style:UIBarButtonItemStylePlain target:self action:#selector(yourButtonAction2)];
NSArray * buttonArray =[NSArray arrayWithObjects:item1,item2 ,nil];
self.navigationItem.rightBarButtonItems =buttonArray;
}
UIView *viewWithButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
//view customization
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
//Button customiztaion
[viewWithButtons addSubview:leftButton];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//Button customiztaion
[viewWithButtons addSubview:rightButton];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:viewWithButtons];
[viewWithButtons release];
I use a category for this:
UIBarButtonItem+WithImageOnly.h:
#import <UIKit/UIKit.h>
#interface UIBarButtonItem (WithImageOnly)
- (id)initWithImageOnly:(UIImage*)image target:(id)target action:(SEL)action;
#end
UIBarButtonItem+WithImageOnly.m:
#import "UIBarButtonItem+WithImageOnly.h"
#implementation UIBarButtonItem (WithImageOnly)
- (id)initWithImageOnly:(UIImage *)image target:(id)target action:(SEL)action {
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
frame = CGRectInset(frame, -5, 0);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [self initWithCustomView:button];
}
#end
Usage:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImageOnly:[UIImage imageNamed:#"image"] target:self action:#selector(someAction:)]
I try to present a modal view (InfoViewController) from a navigation view controller(DateViewController).
I add a toolbar on top of InfoViewContoller's view. Now I want to add a title "Info" and a "Done" button on the toolbar.(The Done button will perform the infoDismissAction method)
Can anyone give me som tips? Thanks a lot!
Here's code of DateViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface DateViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
DateViewController.m
- (IBAction)modalViewAction:(id)sender{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (void)dealloc{
if (self.infoViewController != nil)
{
[infoViewController release];
}
[super dealloc];
}
- (void)viewDidLoad{
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonSystemItemPlay target:self action:#selector(modalViewAction:)] autorelease];
[modalBarButtonItem release];
[super viewDidLoad];
}
Here's InfoViewController.m
- (IBAction)infoDismissAction:(id)sender{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
[backButton release];
[super viewDidLoad];
}
Try this code.
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc] initWithTitle:#"INFO" style:UIBarButtonItemStyleBordered target:self action:#selector(InfoAction:)] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"DONE" style:UIBarButtonItemStyleBordered target:self action:#selector(doneAction:)];
NSArray *barButton = [[NSArray alloc] initWithObjects:flexibleSpace,infoButton,flexibleSpace,doneButton,nil];
[toolBar setItems:barButton];
[self.view addSubview:toolBar];
[toolBar release];
[barButton release];
barButton = nil;
[super viewDidLoad];
}
I'd recommend setting up another UINavigationController with your InfoViewController and present the navigation controller as your modal view.
To answer your question you'd want to fill in your UIToolbar like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
UIBarButtonItem* bbiInfo = [[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonItemStyleBordered target:self action:#selector(tappedInfoButton)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* bbiDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(tappedDoneButton)];
NSArray* items = [[NSArray alloc] initWithObjects:bbiInfo, flexibleSpace, bbiDone, nil];
[toolBar setItems:items];
[items release];
[bbiInfo release];
[flexibleSpace release];
[bbiDone release];
}
Thanks to all. I found really working code. It looks like:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(settingsClick) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem* theSettingsButton =[[UIBarButtonItem alloc]initWithCustomView:infoButton];
[self.toolbar setItems:[NSArray arrayWithObjects:theSettingsButton,nil]];
[theSettingsButton release];
}
This works:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)] autorelease];
}
-(void)settingsClick
{
NSLog(#"Hello");
}
You hadn't closed your viewDidLoad method.
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)]autorelease];
NSMutableArray * arr = [NSMutableArray arrayWithObjects:theSettingsButton, nil];
[self.toolbar setToolbarItems:arr animated:YES];
try this
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)]autorelease];
NSArray * arr = [NSArray arrayWithObjects:theSettingsButton, nil];
[toolbar setItems:arr animated:YES];
[self.view addSubview:toolbar];
[toolbar release];
I've searched around and I can't seem to figure it out. I'm sure many people will have links for me and such, which I've most likely already looked at. But if someone could please just show me code to do the following:
I'd like to have a left arrow in my UINavigationBar as a "Back" UINavigationItem. How can I do this? Here is my current code in my UIViewController:
theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:#selector(backButtonSelected:)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Options" style:UIBarButtonItemStyleBordered target:nil action:#selector(optionsButtonSelected:)];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Title"];
item.leftBarButtonItem = leftButton;
item.hidesBackButton = YES;
item.rightBarButtonItem = rightButton;
[self.view addSubview:theBar];
I think this may be what you're looking for.
// in .h file
#property (nonatomic, retain) UINavigationBar *navBar;
// in .m file just below #implementation
#synthesize navBar;
// within .m method
navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
navBar.barStyle = UIBarStyleBlack;
UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:#"Nav Bar Title"];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:#selector(backButtonSelected:)];
title.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:nil
action:#selector(showActionSheet:)];
title.rightBarButtonItem = rightButton;
[navBar pushNavigationItem:title animated:YES];
[self.view addSubview:navBar];
You should use an UINavigationController in order to pop/push UIViewControllers on your screen. The navigation controller will add an UINavigationBar to your UIViewControllers automatically so you will not need to create them as you did.
Here is a sample. I didn't looked for memory leaks.
In the app delegate you'll find this method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
MainVC *mainVC = [[MainVC alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
MainVC is a UIViewController that represents the level 1 of the hierarchy. in it i have
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view.backgroundColor = [UIColor redColor];
self.title = #"MainVC";
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"SecondLevel" style:UIBarButtonItemStyleBordered target:self action:#selector(secondLevelSelected:)];
self.navigationItem.rightBarButtonItem = rightButton;
}
- (void) secondLevelSelected:(id)sender
{
SecondVC *secondVC = [[SecondVC alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
}
SecondVC is another UIViewController that represents the second level of the hierachy. Here you will find the back button that you want.
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
self.view.backgroundColor = [UIColor greenColor];
self.title = #"SecondVC";
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:#"FirstLevel" style:UIBarButtonItemStyleBordered target:self action:#selector(leftBtnSelected:)];
self.navigationItem.leftBarButtonItem = leftBtn;
}
- (void) leftBtnSelected:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
i got the following code:
- (id)init {
if (self = [super init]) {
self.title = #"please wait";
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"star.png"] style:UIBarButtonItemStylePlain target:self action:#selector(buttonFavoriteClicked:)];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
but my button looks still like a Button with UIBarButtonItemStyleBordered
is there a way to set a button with plain style at this position?
Create a UIButton in interface builder, make it look like exactly what you want. Create an outlet for in in your .h:
IBOutlet UIButton * _myButton;
Then set it as your right bar button item using a custom view, which will eliminate the border:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_myButton];
This works because UIButton is a subclass of UIView.
Try this instead:
- (id)init {
if (self = [super init]) {
self.title = #"please wait";
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[button setImage:[UIImage imageNamed:#"star.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
Hope it helps.
Or do this in code without IB:
// add setting button to nav bar
UIImage* settingsGearImg = [UIImage imageNamed:#"settings_gear.png"];
CGRect frameimg = CGRectMake(0, 0, settingsGearImg.size.width, settingsGearImg.size.height);
UIButton *settingsButton = [[UIButton alloc] initWithFrame:frameimg];
[settingsButton setBackgroundImage:settingsGearImg forState:UIControlStateNormal];
[settingsButton addTarget:self action:#selector(settingsButtonAction) forControlEvents:UIControlEventTouchUpInside];
[settingsButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *settingButtonItem =[[UIBarButtonItem alloc] initWithCustomView:settingsButton];
self.navigationItem.leftBarButtonItem = settingButtonItem;
Results in:
When using this icon image (it's white on a white background so isn't visible here - link is: http://i.stack.imgur.com/lk5SB.png):
It's weird but it works. Say "No" to images/outlets! You shouldn't even set the UIBarButtonItemStylePlain property. The trick is to place button into UIToolBar with some special attributes:
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Cool plain bar"];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.01f)];
tools.clipsToBounds = NO;
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshTableView)];
[buttons addObject:bi];
[tools setItems:buttons animated:NO];
UIBarButtonItem *rightButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
item.rightBarButtonItem = rightButtons;
item.hidesBackButton = YES;
[myCoolNavigationBar pushNavigationItem:item animated:NO];
Although Frank is right, this code should be in viewDidLoad, the issue here is what BarButtons look like. If you want it to look different, you need to use a different type of UIView. You can add a UIButton to a UIToolbar just fine.
-(void)viewDidLoad {
[super viewDidLoad];
self.title = #"please wait";
self.navigationItem.rightBarButtonItem = [[[UIButton alloc] init] autorelease];
}
Edit:
Sorry, you wanted a plain UIBarButtonItem. I don't believe you can do this with a UINavigationBar. You could try adding a UserInteractionEnabled UILabel as the rightBarButtonItem, I'm not sure if it will work or not.
Seems this isn't currently possible.
sbwoodside has a solution.
Why not try UI7Kit? Works well for me, easy to use.
UIImage *img = [UIImage imageNamed:#"white_star.png"];
CGSize itemSize = CGSizeMake(20, 20);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[img drawInRect:imageRect];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIBarButtonItem *barButton = [[UIBarButtonItem alloc ] initWithImage:img style:UIBarButtonSystemItemAdd target:self action:#selector(favoriteButtonClicked)];
barButton.style = UIBarButtonItemStyleBordered;
Try this,
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"more.png"] style:UIBarButtonItemStylePlain target:self action:#selector(didPressButton)];