I have this portion of code to pull up the dictionary if a word is searched:
- (IBAction)searchButtonPressed:(id)sender
{
NSString *searchTerm = self.searchTextField.text;
if([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:searchTerm])
{
UIReferenceLibraryViewController *referenceLibraryVC = [[UIReferenceLibraryViewController alloc] initWithTerm:searchTerm];
[self presentModalViewController:referenceLibraryVC animated:NO];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Word not found" message:#"no definition" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
however, it only seems to work on the main view in the xib and covers the entier iPad screen. How can I get it so that it only opens up in a subview, just a portion of the screen?
Use a UIPopoverController, like it is used in native applications.
self.popover = [[UIPopoverController alloc] initWithContentViewController:referenceLibraryVC];
[self.popover presentPopoverFromRect:[sender frame] inView:[sender superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
You will need to retain the popover controller until it is dismissed. Set the delegate and you can listen when it is dismissed so you can release it.
Related
I am new in iOS and I am facing the problem regarding to dismiss the UIAlertview. I am showing Image in UIAlertview and I am using long press gesture to call the alertview. My code is like this
ImageName.userInteractionEnabled=YES;
UILongPressGestureRecognizer *longpressgestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
[longpressgestureRecognizer addTarget:self action:#selector(imgLongPressed:)];
longpressgestureRecognizer.delegate = self;
[ImageName addGestureRecognizer: longpressgestureRecognizer];
- (void) imgLongPressed:(UILongPressGestureRecognizer*)sender
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 282)];
UIImage *wonImage = ImageName.image;
imageView.contentMode=UIViewContentModeCenter;
[imageView setImage:wonImage];
alertView = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alertView setValue:imageView forKey:#"accessoryView"];
}else{
[alertView addSubview:imageView];
}
[alertView show];
[self performSelector:#selector(dismiss:) withObject:alertView afterDelay:1.0];
}
-(void)dismiss:(UIAlertView*)alert
{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
This code give me output like this Image
I need to dismiss the UIAlertview on the touch of UIViewController or UIScrollView.
UIAlertView is depreciated. It is generally not advised to use depreciated elements since they might cause some problems with latest versions of iOS. For the given functionality, I think it's better to use thirdparty AlertViews like this one: https://cocoapods.org/pods/SCLAlertView-Objective-C
I am trying to develop a simple application which is going to work with the photos the user takes. By following https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Articles/TakingPicturesAndMovies.html I am using a UIImagePickerController in order to take the snapshot. I have a basic application with a single, empty view controller and in the viewDidAppear method I do the following:
- (void)viewDidAppear:(BOOL)animated
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Error" message: #"Can't use the camera!" delegate: nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
[self presentViewController: cameraUI animated: YES completion:NULL];
}
The resulting view becomes something like:
As you can see the front-rear camera change button doesn't show completely and the UIImagePickerController does not fit into the screen. Should I additionally resize before presenting or am I doing something incorrectly?
Background: On iPad, I have a button which when tapped, showed a UIActionSheet. This action sheet has 2 options, camera and gallery. Camera when tapped, pulled up a camera and everything works fine. Gallery when tapped, suppose to show a popover with user's photos in it.
Problem: On iPad, UIActionSheet acts like a popover. Which when presenting, another popover cannot come into view. Error:Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.
My code:
Setting Action Sheet
- (void)imageButtonTapped:(UIButton *)sender
{
if (_commentObject.image){
_actionSheet = [[UIActionSheet alloc] initWithTitle:#"Action" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Remove" otherButtonTitles:nil];
_actionSheet.tag = ACTION_IMAGE_REVIEW_TAG;
}else{
_actionSheet = [[UIActionSheet alloc] initWithTitle:#"Image Source" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Gallery", nil];
_actionSheet.tag = ACTION_IMAGE_SOURCE_TAG;
}
if (_isPad) {
[_actionSheet showFromRect:_imageButton.frame inView:_scrollViewContent animated:YES];
}else{
[_actionSheet showInView:self.view];
}
}
Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (actionSheet.tag) {
case ACTION_IMAGE_SOURCE_TAG:
switch (buttonIndex) {
case 0:
[self pickImage:YES];
break;
case 1:
[self pickImage:NO];
break;
default:
break;
}
break;
}
Executing
- (void)pickImage:(BOOL)fromCamera
{
if (fromCamera) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController* cameraPickerController = [[UIImagePickerController alloc] init];
cameraPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPickerController.delegate = self;
[self presentViewController:cameraPickerController animated:YES completion:nil];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Camera Unavailable" message:#"Your Device does not support Cameras" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
}else{
UIImagePickerController *galleryPickerController = [[UIImagePickerController alloc] init];
galleryPickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
galleryPickerController.delegate = self;
if (_isPad) {
if ([_actionSheet isVisible]) {
[_actionSheet removeFromSuperview];
UIPopoverController *imagePickerPopover = [[UIPopoverController alloc] initWithContentViewController:galleryPickerController];
[imagePickerPopover presentPopoverFromRect:_imageButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}else{
[self presentViewController:galleryPickerController animated:YES completion:nil];
}
}
}
Question: I tried removing the action sheet from view, and tried dismissing it before executing pickImage. None of that works. How do I present the gallery?
Your Problem: Error:Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.
make Object of UIPopoverController in your .h file class. Make sure that your #property for your UIPopoverController is strong instead of weak.
Check this
UIPopoverController: dealloc reached while popover is still visible
UIPopovercontroller dealloc reached while popover is still visible
FYI, This was a comment and OP asked to put it as an answer. Because this solved the issue.
It's happening because you don't have a reference to your UIPopoverController. Have a strong reference for your imagePickerPopover and then try. No problem will occur.
you can try this:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if ([actionSheet isVisible]) {
[actionSheet dismissWithClickedButtonIndex:0 animated:NO];
}
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIImagePickerController *galleryPickerController = ......
}
}
i have a popoverview issues while click and open the camera for ipad,i wrote code like that
-(IBAction)business_takephotobtnClicked // click the button show the popoverview
{
NSLog(#"business_takephotobtnClicked");
appdelegate.takePhoto=2;
popover = [[UIPopoverController alloc]
initWithContentViewController:imgclass];
popover.popoverContentSize = CGSizeMake(138,66);
[popover presentPopoverFromRect:popbtn_business.bounds inView:popbtn_business
permittedArrowDirections:UIPopoverArrowDirectionUp +
UIPopoverArrowDirectionLeft
animated:YES];
}
-(IBAction) takePhoto:(id)sender // to open the camera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
self.contentSizeForViewInPopover=CGSizeMake(138,66);
UIPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:self.UIPicker animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Camera is not available" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
before click the button the popover like that show
while click the take photo(Neem foto button).THE POPOVERVIEW SIZE IS AUTOMATICALLY EXTENTED like taht
But i need a same size popoverview while open the camera also
Thanks in Advance......
instead of using XIB use to create the camera view programatically and do like the following
-(IBAction)popbtn_Click:(id)sender
{
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 230, 180)];
popoverView.backgroundColor = [UIColor whiteColor];
take_btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[take_btn setTitle:#"Take" forState:UIControlStateNormal];
take_btn.frame=CGRectMake(2,2, 250, 60);
[take_btn addTarget:self action:#selector(take_btnclick:) forControlEvents:UIControlEventTouchUpInside];
[popoverView addSubview:take_btn];
}
-(void)take_btnclick:(id)sender
{
[popoverController dismissPopoverAnimated:YES];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:self.UIPicker animated:YES];
[popoverController dismissPopoverAnimated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Camera is not available" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
if (popoverController != nil)
{
[popoverController dismissPopoverAnimated:YES];
}
}
I want to add the feature that when i tap on a specific textfield, then there is a Dropdown / TableView in front of the gui and i can select a value and then the selected value is then inserted into the textfield?
How to achieve this in the easiest way?
Depends, if this is on the iPad, you can use UIPopoverController and present a UITableViewController inside it.
If not, you can present a UIActionSheet and add UIPickerView or a UITableView inside it...in this example, I am creating a UIActionSheet and placing a UIPickerView inside it:
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? #"\n\n\n\n\n\n\n\n\n" : #"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:[NSString stringWithFormat:#"%#%#", title, [entry objectForKey:#"Name"]]
delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"Ok", nil];
[actionSheet setDelegate:self];
[actionSheet setTag:row];
pv = [[UIPickerView alloc] init];
[pv setShowsSelectionIndicator:YES];
[pv setDelegate:self];
[pv setTag:28];
[actionSheet addSubview:pv];
[actionSheet showInView:[[[self navigationController] tabBarController] view]];
[pv release];
Notice the "\n"s, you need those to make room for the Picker view. Here's the result: