I try to implement customkeyboard with UISearchBar .first of all my mainclass.h where i use UISearchBar is look like this
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#class PashtoKeyboard;
#class SearchBar;
#interface MainViewController : UIViewController<UISearchBarDelegate > {
IBOutlet SearchBar *searchBar;
PashtoKeyboard *pashtoKeyboard;
}
#property(nonatomic,retain) PashtoKeyboard *pashtoKeyboard;
#property (nonatomic,retain) SearchBar *searchBar;
#end
Now my mainclass.m
#import "MainViewController.h"
#import "PashtoKeyboard.h"
#import "SearchBar.h"
#implementation MainViewController
#synthesize pashtoKeyboard,searchBar;
- (void)viewDidLoad
{
[super viewDidLoad];
//Create Pashto Keyboard
PashtoKeyboard *pashtoKey = [[PashtoKeyboard alloc] initWithFrame:CGRectMake(0.0, 460.0-216.0, 320.0, 216.0)];
[pashtoKey loadView];
//[self.view addSubview:hebKey];
[pashtoKey addTarget:self action:#selector(pashtoKeyboard_clicked:) forControlEvents:1001];
[pashtoKey addTarget:self action:#selector(pashtoKeyboardBackspace_clicked:) forControlEvents:1002];
[pashtoKey addTarget:self action:#selector(pashtoKeyboardEnter_clicked:) forControlEvents:1003];
[self setPashtoKeyboard:pashtoKey];
//[hebKey setHidden:YES];
[pashtoKey release];
searchBar.inputView = pashtoKey;
searchBar.delegate=self;
searchBar.userInteractionEnabled=YES;
}
#pragma mark Pashto Keyboard Methods
- (void) pashtoKeyboard_clicked:(id)sender{
[searchBar setText:[pashtoKeyboard keyboardText]];
}
- (void) pashtoKeyboardBackspace_clicked:(id)sender{
[searchBar setText:[pashtoKeyboard keyboardText]];
}
- (void) pashtoKeyboardEnter_clicked:(id)sender{
[searchBar setText:[pashtoKeyboard keyboardText]];
}
In SearchBar.h
#import <UIKit/UIKit.h>
#interface SearchBar : UISearchBar {
}
#property (readwrite, retain) UIView *inputView;
#end
in SearchBar.m
#import "SearchBar.h"
#implementation SearchBar
#synthesize inputView;
- (void)dealloc {
[super dealloc];
}
#end
And then finally i have PashtoKeyboard.h and PashtoKeyboard.m where i create my custom keyboard,due to large coding i not show these classes code here i tested its with textveiw and textfield.
But it not works with UISearchBar .can some one guide me how to do this.thanx
You need to set the inputView of the UITextField that the UISearchBar is using in order to get the user input. You can do this by searching through the subviews of the search bar and finding the text field. Add this to the viewDidLoad method of your mainclass:
for (UIView *view in searchBar.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField *textField = (UITextField *)view;
textField.inputView = // Your custom keyboard (PashtoKeyboard)
break;
}
}
Related
I have a AuthButtons.h header:
#import <UIKit/UIKit.h>
#interface AuthButtons : UIButton
#property (nonatomic, weak) UIColor *backgroundColor;
#end
The implementation AuthButtons.m class is as follows:
#import "AuthButtons.h"
#implementation AuthButtons
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
backgroundColor = [UIColor blueColor];
}
- (UIColor *)backgroundColor
{
return [UIColor whiteColor];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Then in my viewcontroller.m class I have:
#interface ViewController ()
#property (nonatomic, weak) AuthButtons *registerButton;
#end
...
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_registerButton = [AuthButtons buttonWithType:UIButtonTypeSystem];
[_registerButton setTitle:#"Register" forState:UIControlStateNormal];
[_registerButton setBackgroundColor:[UIColor whiteColor]];
_registerButton.frame = CGRectMake(0.0, 0.0, self.screenWidth / 2.0, 100.0);
[_registerButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
It keeps crashing when I try to create the button. What am I missing?
Your button is weak and not added as a subview so its deallocated right away
Use strong instead of weak(i always use strong type when using buttons in ARC enabled class,not sure whether its a bad habbit :-))
#property (nonatomic, strong) AuthButtons *registerButton;
and add it as your subview([self.view addSubview:_registerButton]).Hope you have implemeted the function called -(void)buttonPressed:(id )sender in your class.
I have two view controllers: view controller and viewcontroller2nd. I have UILabel in one of them and would like to change it when the button( named Go) in the viewcontroller2nd is clicked. I am using delegates and protocols to do it.
The code looks like this:
ViewController.h
#import <UIKit/UIKit.h>
#import "ViewController2nd.h"
#interface ViewController : UIViewController <SecondViewControllerDelegate>
{
IBOutlet UILabel *lbl;
ViewController2nd *secondview;
}
-(IBAction)passdata:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "ViewController2nd.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) changeLabel:(NSString*)str{
lbl.text = str;
}
-(IBAction)passdata:(id)sender{
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
[self presentViewController:second animated:YES completion:NULL];
}
#end
Viewcontroller2nd.h
#import <UIKit/UIKit.h>
#protocol SecondViewControllerDelegate <NSObject>
#optional
-(void) changeLabel:(NSString*)str;
#end
#interface ViewController2nd : UIViewController{
IBOutlet UIButton *bttn;
id <SecondViewControllerDelegate> delegate;
}
#property (retain) id delegate;
-(IBAction)bttnclicked;
-(IBAction)back:(id)sender;
#end
ViewController2nd.m
#import "ViewController2nd.h"
#import "ViewController.h"
#interface ViewController2nd ()
#end
#implementation ViewController2nd
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)bttnclicked{
[[self delegate] changeLabel:#"Hello"];
}
-(IBAction)back:(id)sender{
[self dismissViewControllerAnimated:YES completion:NULL];
}
#end
The passing of the control between the two views is working correctly. However, when i click the go button in the viewcontroller2nd, it doesn't change the value of the label to Hello. What is wrong with the code? Need some guidance.
It seems you never set the delegate.
You can do it in your passData method :
-(IBAction)passdata:(id)sender{
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
second.delegate = self;
[self presentViewController:second animated:YES completion:NULL];
}
Hm, try the following in your changeLabel delegate method:
-(void) changeLabel:(NSString*)str{
lbl.text = str;
[lbl setNeedsDisplay];
}
EDIT:
Other than that:
Your label is a IBOutlet, right? Did you connect the label in your xib with the IBOutlet property (i.e. lbl) correctly in interface builder with the files owner
If you want to do this by using delegates then change passdata like:
-(IBAction)passdata:(id)sender
{
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
[second setDelegate:self];
[self presentViewController:second animated:YES completion:NULL];
}
You can do this without delegates.
Just create a object of ViewController in secondView and synthesize it like:
#import "ViewController.h"
#interface ViewController2nd : UIViewController
{
IBOutlet UIButton *bttn;
ViewController *parent;
}
#property (nonatomic, assign) ViewController *parent;
-(IBAction)bttnclicked;
-(IBAction)back:(id)sender;
#end
and in the passdata change it like
-(IBAction)passdata:(id)sender
{
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
[second setParent:self];
[self presentViewController:second animated:YES completion:NULL];
}
and change the bttnclicked like:
-(IBAction)bttnclicked
{
[parent changeLabel:#"Hello"];
}
I wrote following for you!
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *label;
#end
#import "ViewController.h"
#import "ViewController2.h"
#interface ViewController () <ViewController2Delegate>
#end
#implementation ViewController
-(void)passdata:(NSString *)data
{
self.label.text = data;
}
- (IBAction)buttonClicked:(id)sender {
ViewController2 *v = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
v.delegate = self;
[self presentViewController:v animated:YES completion:nil];
}
#end
#import <UIKit/UIKit.h>
#protocol ViewController2Delegate <NSObject>
- (void)passdata:(NSString*)data;
#end
#interface ViewController2 : UIViewController
#property(assign, nonatomic) id<ViewController2Delegate> delegate;
#end
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (IBAction)buttonClicked:(id)sender {
[self.delegate passdata:#"hello"];
}
- (IBAction)backButtonClicked:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
You don't need to import the header "ViewController2nd.h" in the both ViewController.h and ViewController.m
You declare the UILabel without property like, #property (strong, nonatomic).
May be, You didn't properly map the UILabel to your .storyboard or .xib file.
You forget to assign the second view controller delegate while calling the controller
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
second.delegate = self;
[self presentViewController:second animated:YES completion:NULL];
If anyone is using storyboard for viewcontrollers navigation.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier hasPrefix:#"view_to_capture"]) {
ViewController2nd *destination = segue.destinationViewController;
destination.delegate = self;
}
}
I'm attempting to implement a pop-up UIPickerView to emulate the behavior when clicking on drop-down field within Safari with limited success so far. I found:
Bottom pop-up UIPicker?
and have attempted to follow this. So far, I have my PopUpPickerView showing, but the UIPickerView itself does not (I just see the view background color showing).
PopUpPickerViewController has a nib file so that when a button is clicked, the 'change' selector is invoked and the UIPickerView should pop up. At the moment, only the background color of the PopUpPickerView pops up.
Any help you could provide would be very much appreciated. Thanks in advance!
My code is as follows:
PopUpPickerViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PopUpPickerView.h"
#define UIColorMakeRGBA(nRed, nGreen, nBlue, nAlpha) [UIColor colorWithRed: (nRed)/255.0f green: (nGreen)/255.0f blue:(nBlue)/255.0f alpha:nAlpha]
#class PopUpPickerView;
#interface PopUpPickerViewController : UIViewController{
NSArray *pickerData;
PopUpPickerView *pickerView;
UIPickerView *picker;
}
-(IBAction)change:(id)sender;
-(void)addSubviewToWindow:(UIView*) addView;
#property(nonatomic, retain) PopUpPickerView *pickerView;
#property(nonatomic, retain) NSArray *pickerData;
#property(nonatomic, retain) UIPickerView *picker;
#end
PopUpPickerViewController.m
#import "PopUpPickerViewController.h"
#import "PopUpPickerView.h"
#implementation PopUpPickerViewController
#synthesize pickerData, pickerView, picker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.pickerView = [[PopUpPickerView alloc] initWithFrame:CGRectMake(0.0, 174.0, 320.0, 286.0)];
self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 70.0, 320.0, 216.0)];
self.pickerView.picker = self.picker;
//[self.picker addTarget:self action:#selector(pickerValueChanged:) forControlEvents:UIControlEventValueChanged];
self.pickerView.parentViewController = self;
[self.pickerView addSubview:self.picker];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)change:(id)sender{
PopUpPickerView *pView = (PopUpPickerView*) pickerView;
[pView animateDatePicker:YES];
}
-(void)addSubviewToWindow:(UIView*) addView{
[self.view addSubview:addView];
}
#end
PopUpPickerView .h
#import <UIKit/UIKit.h>
#import "PopUpPickerViewController.h"
#class PopUpPickerViewController;
#interface PopUpPickerView : UIView<UIPickerViewDelegate, UIPickerViewDataSource>{
UIPickerView *picker;
PopUpPickerViewController *parentViewController;
NSArray *pickerData;
}
-(void)animateDatePicker:(BOOL)show;
#property(nonatomic, retain) UIPickerView *picker;
#property(nonatomic, retain) NSArray *pickerData;
#property(nonatomic, retain) PopUpPickerViewController *parentViewController;
#end
PopUpPickerView.m
#import "PopUpPickerView.h"
#import "PopUpPickerViewController.h"
#implementation PopUpPickerView
#synthesize picker, pickerData, parentViewController;
- (void)animateDatePicker:(BOOL)show {
pickerData = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", nil];
self.picker.delegate = self;
self.picker.dataSource = self;
CGRect screenRect = self.frame;
CGSize pickerSize = [self.picker sizeThatFits:CGSizeZero];
CGRect startRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height,
pickerSize.width, pickerSize.height);
CGRect pickerRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height,
pickerSize.width,
pickerSize.height);
self.picker.frame = pickerRect;
self.backgroundColor = UIColorMakeRGBA( 255, 125, 64, 0.7f - (int)show * 0.7f );
if ( show ) {
self.picker.frame = startRect;
PopUpPickerViewController *controller = (PopUpPickerViewController*) self.parentViewController;
[controller addSubviewToWindow:self];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationDelegate:self];
self.backgroundColor = UIColorMakeRGBA( 255, 125, 64, 0.0f + (int)show * 0.7f );
if ( show ) {
self.picker.frame = pickerRect;
} else {
[UIView setAnimationDidStopSelector:#selector(slideDownDidStop)];
self.picker.frame = startRect;
}
[UIView commitAnimations];
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.pickerData objectAtIndex:row];
}
#end
It turned out that the answer was to do with the coordinates at which the UIPickerView was placed. The UIPickerView was being placed at the bottom of the PopUpPickerView which meant that it was offscreen. After adjusting the coordinates, it all worked out fine.
I have used SBTableAlert several times and it seems to provide what you require but with a UITableView instead of a UIPickerView. Take a look and hope it helps in your project.
Git Repo
I have a ViewController with ScrollView SubView and those subview has a subview in it, below is the hierarchy :
AppDelegate - MyViewController - ScrollView(from xib) - MyScrollViewSubClass
// MyViewController :
// .h
#interface MyViewController : UIViewController<MyScrollViewSubClassDelegate,UIScrollViewDelegate> {
UIScrollView *scrollView; // Loaded from xib
}
#property(nonatomic,retain) IBOutlet UIScrollView *scrollView;
#end
// .m
#implementation MyViewController
#synthesize scrollView;
- (void)viewDidLoad {
[super viewDidLoad];
scrollView.delegate = self;
MyScrollViewSubClass *myScrollView = [[MapScrollView alloc] init];
myScrollView.delegate = self;
myScrollView.myDelegate = self;
scrollView addSubview:mapScrollView];
}
// The MyScrollViewSubClassDelegate #required method
- (void) onDoubleTapLocation: (CGPoint)tapPoint
{
// Working,,,NSLog shown that this line is Working...
}
// MyScrollViewSubClass + Delegate
// .h
// Protocol
#protocol MyScrollViewSubClasswDelegate <NSObject>
#required
- (void) onDoubleTapLocation: (CGPoint)tapPoint;
#end
//Interface
#protocol MyScrollViewSubClass;
#interface MyScrollViewSubClass : UIScrollView <UIScrollViewDelegate> {
id <MyScrollViewSubClasswDelegate> myDelegate;
// Another vars..
}
#property (nonatomic,assign) id<MyScrollViewSubClasswDelegate> myDelegate;
- (void)handleDoubleTap;
#end
// .m
#implementation MyScrollViewSubClass
#synthesize myDelegate;
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
// Another Code Here...
self.delegate = self; // delegate for UIScrollView
self.myDelegate = self;
//Another Code Here...
}
return self;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(isDoubleTap)
{
//Call handle double tap
[self handleDoubleTap];
}
}
- (void)handleDoubleTap {
[self.myDelegate onDoubleTapLocation: tapLocation];
}
What Happen is the double tap is working on MyViewController, but now i can't scroll and pinch zoom my MyScrollViewSubClass from MyViewController, any critics, comment or better method are welcome..
Resolved :
I assign delegate on MyViewController twice
was :
myScrollView.delegate = self;
myScrollView.myDelegate = self;
Should be :
myScrollView.myDelegate = self;
Regards,
Ferry Hattawidian
Resolved :
I assign delegate on MyViewController twice
was :
myScrollView.delegate = self;
myScrollView.myDelegate = self;
Should Be :
myScrollView.myDelegate = self;
Everything else is running well... Thanks guys...
I am using code from Ed Marty's answer for the question here but am having real trouble with a few bits.
On the click of a button I have got the datepicker appearing, but the 'done' button however isn't. I am also getting an error from the line:
[delegate datePickerController:controller didPickDate:datePicker.date];
Error message:
'controller' undeclared (first use in this function)
All in all I have 6 files:
ModalDatePickerViewController.m
ModalDatePickerViewController.h
ModalDatePickerAppDelegate.m
ModalDatePickerAppDelegate.h
DatePickerController.m
DatePickerController.h
My DatePickerController.h is looking like:
#import <UIKit/UIKit.h>
#class DatePickerController;
#protocol DatePickerControllerDelegate
- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date;
#end
#interface DatePickerController : UIViewController {
UIDatePicker *datePicker;
NSObject <DatePickerControllerDelegate> *delegate;
}
#property (nonatomic, retain) UIDatePicker *datePicker;
#property (nonatomic, assign) NSObject <DatePickerControllerDelegate> *delegate;
#end
and the DatePickerController.m:
#import "DatePickerController.h"
#implementation DatePickerController
#synthesize datePicker;
#synthesize delegate;
- (void) loadView {
self.view = [[[UIView alloc] init] autorelease];
self.datePicker = [[[UIDatePicker alloc] init] autorelease];
[self.view addSubview:self.datePicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Done" forState:UIControlStateNormal];
button.center = CGPointMake(160,230);
[button addTarget:self action:#selector(done) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
- (void) done {
[delegate datePickerController:controller didPickDate:datePicker.date];
}
- (void) dealloc {
[datePicker release];
[super dealloc];
}
#end
On the main view I have a button that call this class as below:
#import "ModalDatePickerViewController.h"
#implementation ModalDatePickerViewController
- (void) pickDate {
DatePickerController *screen = [[[DatePickerController alloc] init] autorelease];
screen.delegate = self;
[self presentModalViewController:screen animated:YES];
}
- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date {
//[self doSomethingWithDate:date];
[controller dismissModalViewControllerAnimated:YES];
}
- (IBAction)HitMe:(id)sender {
[self pickDate];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
and:
#import <UIKit/UIKit.h>
#import "DatePickerController.h"
#interface ModalDatePickerViewController : UIViewController <DatePickerControllerDelegate> {
}
- (IBAction)HitMe:(id)sender;
#end
On this line:
[delegate datePickerController:controller didPickDate:datePicker.date];
try replacing controller with self.