ABPeoplePickerNavigationController change Cancel button text to Done and still call delegate method? - ios

If the text itself can't be changed and the button needs to be replaced then please also explain how to make the delegate method
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
still get called from replacement button?
Any help is greatly appreciated.

After some fiddling this worked:
Include UINavigationControllerDelegate in object .h file. Add the following to .m file:
- (void) showAddressBook {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
// use delegate to change cancel text to done on picker that pops up
picker.delegate = self;
[self presentViewController:picker animated:true completion:nil];
}
// replace button now that controller is initialized
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if([navigationController isKindOfClass:[ABPeoplePickerNavigationController class]]) {
UIBarButtonItem *obbi = navigationController.topViewController.navigationItem.rightBarButtonItem;
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:obbi.target action:obbi.action];
navigationController.topViewController.navigationItem.rightBarButtonItem = bbi;
}
}

Related

Navigation Bar's Button Item Not Clickable on iPad

In my app, I use the old GKImage library for adding pictures and being able to custom crop them easily. This works pretty well, except when it comes to iPad. On iPhone, everything works great. The main issue is this:
When I use an iPad, none of the barButtonItems are clickable.
Here is my code for this view:
- (void)_actionCancel{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)_actionUse{
NSLog(#"PRESSED");
_croppedImage = [self.imageCropView croppedImage];
[self.delegate imageCropController:self didFinishWithCroppedImage:_croppedImage];
}
- (void)_setupNavigationBar{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(_actionCancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Use"
style:UIBarButtonItemStylePlain
target:self
action:#selector(_actionUse)];
}
- (void)_setupCropView{
self.imageCropView = [[GKImageCropView alloc] initWithFrame:self.view.bounds];
[self.imageCropView setImageToCrop:sourceImage];
[self.imageCropView setResizableCropArea:self.resizeableCropArea];
[self.imageCropView setCropSize:cropSize];
[self.view addSubview:self.imageCropView];
}
#pragma mark -
#pragma Super Class Methods
- (id)init{
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
self.title = #"Choose Photo";
[self _setupNavigationBar];
[self _setupCropView];
[self.navigationController setNavigationBarHidden:NO];
}
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
self.imageCropView.frame = self.view.bounds;
}
The code used to present this controller is:
self.imagePicker = [[GKImagePicker alloc] init];
self.imagePicker.cropSize = CGSizeMake(280, 280);
self.imagePicker.delegate = self;
self.imagePicker.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePicker.imagePickerController animated:YES completion:nil];
Is it an issue with presenting the view controller?

Custom Views aren't dismissing keyboards?

Have tried [[self jotNotes] resignFirstResponder]; , have tried [self endEditing:YES];
So I have my NoteViewController, which inherits UIViewController, and tries to implement the delegate like so
#interface NOTEController : UIViewController <UITextViewDelegate>
#end
#implementation NOTEController
-(id)init {
self = [super init];
if (self) {
// self.delegate = self; //doesnt let me set this, so i assume i do not do that here
NOTEControllerView * mainView = [[NOTEControllerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = mainView; //just a plain custom uiview subclass its boring and not special
}
return self;
}
#end
and then in the mainView, i have a bunch of sub-views that are basically a square with a UITextView inside.
the squares class is like this, and they're the ones im trying to dismiss the keyboard from, heres where i set the delegate, the dismissKB method, and the UITextView code. Currently, it will log my keyboard method when pressing the done button, but the keyboard is still present. Would really appreciate if anyone could help me understand why
#interface NOTESubview : UIView <UITextFieldDelegate>
#property (nonatomic, weak) id<UITextFieldDelegate> delegate;
-(UITextView *)jotNotes;
#end
#implementation NOTESubview
-(id)initWithFrame:(CGRect)arg1 {
self = [super initWithFrame:arg1];
if (self) {
self.delegate = self;
[self addSubview:[self jotNotes]];
}
return self;
}
-(UITextView *)jotNotes {
UITextView * jotNotes = [[UITextView alloc] initWithFrame:CGRectMake(0, self.frame.size.height/5.7, self.frame.size.width, self.frame.size.height - self.frame.size.height/5.7)];
UIToolbar* keyboardTextViewBar = [[UIToolbar alloc] init];
keyboardTextViewBar.barStyle = UIBarStyleDefault;
[keyboardTextViewBar sizeToFit];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self
action:#selector(dismissKB:)];
[keyboardTextViewBar setItems:[NSArray arrayWithObjects:flexSpace, doneButton, nil]];
jotNotes.inputAccessoryView = keyboardTextViewBar;
jotNotes.delegate = self.delegate;
return jotNotes;
}
-(void)dismissKB:(UIBarButtonItem *)sender {
//this will log, so im not sure why it wont resign the board no matter what i try
NSLog(#"keyboard attempted to dismiss by %#", sender);
[[self jotNotes] resignFirstResponder];
}
I suspect that when the dismissKB method is called, its actually not the one who is currently the first responder.
However, there's a trick where you can just "dismiss the keyboard" from anywhere in your app. You might wanna give it a try:
[[[[UIApplication sharedApplication] delegate] window] endEditing:YES];
Add the following line where you are trying to dismiss the keyboard:
[self endEditing:YES];

Send SMS to Selected Contacts in iPhone

I want to send SMS invite to multiple phone numbers selected from the contact list, I tried to use ABPeoplePicker but it is not coming correctly, my contact list is always showing blank.
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(showAddressBook)];
self.navigationItem.rightBarButtonItem = addButton;
}
#pragma mark - Private method implementation
-(void)showAddressBook{
_addressBookController = [[ABPeoplePickerNavigationController alloc] init];
[_addressBookController setPeoplePickerDelegate:self];
[self presentViewController:_addressBookController animated:YES completion:nil];
}
I feel I am doing something wrong, can anyone help me.

Dismiss keyboard when default ios mail app opens

In my app I display the MFMailComposeViewController to display default mail app. How to dismiss the keyboard which opens up in the controller?
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
[firstResponder resignFirstResponder];
I hope This Will Help You.. :)
Unfortunately it look's like even if we try to force close the keyboard using endEditing; that function takes no effect. On my device it doesn't seem like the keyboard can be dismissed.
Below would have been the answer that should have worked:
Display the MFMailComposeViewControllerwith a completion and call endEditing: on the MFMailComposeViewController's view.
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
[mailVC.view endEditing:YES];
}];
UPDATE:
Wouldn't suggest doing this but, here is a hackish way that works:
Objective-C:
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
UITextField *a = [[UITextField alloc] init];
[mailVC.view addSubview:a];
[a becomeFirstResponder];
[mailVC.view endEditing:YES];
}];
Swift:
let mailVC = MFMessageComposeViewController()
self.presentViewController(mailVC, animated: true) { () -> Void in
let a = UITextField()
mailVC.view.addSubview(a)
a.becomeFirstResponder()
mailVC.view.endEditing(true)
}
In your viewcontroller.h add:
#property (nonatomic) UITapGestureRecognizer *tapRecognizer;
Now in the .m file, add this to your ViewDidLoad function:
- (void)viewDidLoad
{
[super viewDidLoad];
//Keyboard stuff
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];
}*
Also, add this function in the .m file:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}

add a back button to UIImagepicker controller navigation nar

i have a UI Image picker and its being shown inside a UI popoverController, i need a back button on UIimage picker navbar, so i can go back to previous popover.
let me explain in details.
1) i have a popup in which i tap and raise a notification, this notification is read on a view controller where i want this camera image picker popup. following is my code
-(void)registerNotificationForCameraSource
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(mediaSourceCallback:) name:NOTIFICATION_FOR_CAMERA_SOURCE object:nil];
}
#pragma mark - Image picker Popover
-(void)mediaSourceCallback:(NSNotification *)notificationObj
{
capturedImages = [[NSMutableArray alloc] init];
pickerObj = [[UIImagePickerController alloc] init];
// [pickerObj setContentSizeForViewInPopover:CGSizeMake(320,480)];
pickerObj.delegate = self;
if ([notificationObj.object isEqualToString:#"UIImagePickerControllerSourceTypeCamera"])
{
pickerObj.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerObj.modalPresentationStyle = UIModalPresentationFullScreen;
pickerObj.showsCameraControls = YES;
[self presentViewController:pickerObj animated:YES completion:nil];
}
else
{
[[UINavigationBar appearanceWhenContainedIn:[ApplicationDiscriptionPopupViewController class], nil] setBarTintColor:[UIColor redColor]];
pickerObj.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(imagePickerBackButtonTapped)];
[pickerObj.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
[pickerObj setNavigationBarHidden:NO animated:YES];
//navigationControllerObj = [[UINavigationController alloc] initWithRootViewController:applicationDiscriptionPopupViewControllerObj];
_popoverControllerObj.popoverContentSize = CGSizeMake(320,480);
pickerObj.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[pickerObj presentedViewController];
[_popoverControllerObj setContentViewController:pickerObj animated:YES];
}
}
-(void)imagePickerBackButtonTapped
{
[pickerObj popToViewController:applicationDiscriptionPopupViewControllerObj animated:YES]; }
pickerObj is UI Image picker
applicationDiscriptionPopupViewControllerObj is view controller of my previous popover view. from where i came. now i wana go back to this view in the popup, when i tap on back button in nav bar.
but i dnt hav any nav bar. plz help
If you are adding a UIImagePickerController via presentViewController inside a UIViewController then you should see the default navigation bar. You should implement :
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UINavigationItem *navBarTopItem;
// you cannot add a custom back button with native back button look like, so use image over
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back_arrow.png"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backAction)];
navBarTopItem.title = #"Albums"; // "Camera
navBarTopItem.leftBarButtonItem = backButton;
}
Add a back button action handler
-(void)backAction{
[self.navigationController popViewControllerAnimated:YES];
}
Hope that answer ur Q.

Resources