How to create a UIAlertView with table view - objective-c

I need to add a tableview inside a UIAlertView in Objective-C.
I tried to add a tableview as a subview of the alert view, but this is not working.
-(IBAction)ShowAlertWithTable
{
UIAlertView *select_dialog ;
select_dialog = [[[UIAlertView alloc] init] retain];
[select_dialog setDelegate:self];
[select_dialog setTitle:#"Alphabets"];
[select_dialog setMessage:#"\n\n\n\n"];
[select_dialog addButtonWithTitle:#"Cancel"];
UITableView * Type_table = [[UITableView alloc]initWithFrame:CGRectMake(20, 45, 245, 90)];
Type_table.backgroundColor = [UIColor redColor];
idType_table.delegate = self;
idType_table.dataSource = self;
[select_dialog addSubview:Type_table];
[Type_table reloadData];
[select_dialog show];
}

Why don't you use this library? It has all what you needs!
https://github.com/mindbrix/SBTableAlert
The above developer is using this 'Library' for AlertView
https://github.com/mindbrix/TSAlertView

The default alert isn't meant to take subviews. Although, you can write your own custom UIView that can meet your needs.

I know it is late to answer the question. I want the same functionality in my application and after a little search I found my answer.
You can use UIAlertView's setValue:<your UITableView> forKey:#"accessoryView" property to add your UITableView in UIAlertView. You can create the UITableview programmatically to set value above. You can create and set any object like UIView, UIImageView etc.
For more detail Please refer this answer

Related

UIView Modal Popup with Embedded UITableView with UIToolbar, Toolbar moves with Table

This is driving me insane! (ios 5+, ARC)
Pretty simple concept here:
I have a UIView with an embedded UITableView, when clicking on a specific cell, I have iOS launch a modalview with a segue.
This modalview, is a UIView with an Embedded UITableView in it, filled with names from a data source. You can choose multiple items in this view (using cellaccessory: checkmark)
The Goal
Get some sort of "Done" button to show up
Ok, so after much running around, I am to understand that Modal windows do not, in fact, allow navigationController items. No toolbars, no Navigationbars by default.
Ok, so I'll create my own.
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger tbHeight = 50;
UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - tbHeight), self.view.frame.size.width, tbHeight)];
tb.translucent = YES;
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneAction:)];
NSArray *barButton = [[NSArray alloc] initWithObjects:flexibleSpace,doneButton,nil];
[tb setItems:barButton];
[self.view addSubview:tb];
barButton = nil;
//....
}
Simple enough right?
Well, the toolbar does show up, for sure. BUT, it doesnt stick to the bottom like its supposed to. In fact, when you scroll the embedded tableview up and down, the UIToolbar goes with it, almost like its stuck on the tablevie somehow.
Ive been looking for hours for solutions, I've come across nothing. Wondering if anybody here has any ideas?
If you need more information, please by all means, enquire :)
It's strange that your toolbar is scrolling with the table if it's a UIViewController subclass, unless you've assigned a UITableView to self.view or something... But since it is, this is what I do when adding a fixed item to a table view:
-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
tb.frame = CGRectMake(0, self.view.bounds.size.height-tb.bounds.size.height+scrollView.contentOffset.y, self.view.bounds.size.width, tb.bounds.size.height);
}

cellForItemAtIndexPath not called

I'm trying to push from a view controller to a collection view controller.
UICollectionViewLayout *collectionViewLayout = [[UICollectionViewLayout alloc] init];
xraylab *xraylabvc = [[xraylab alloc]initWithCollectionViewLayout:collectionViewLayout];
[self.navigationController pushViewController:xraylabvc animated:YES];
The collection view controller will be displayed with a blank black screen.
numberOfSectionsInCollectionView returns 1
numberOfItemsInSection returns a non-zero integer
My problem is that cellForItemAtIndexPath is not called.
Delegate and datasource of the collection view are set.
Any suggestions?
numberOfItemsInSection should return rowcount > 0 for cellForItemAtIndexPath to execute
Check Delegate and datasource of the collection view are set
I had the same problem and it was driving me crazy here is the code that worked.
UICollectionViewFlowLayout *CollLayout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *AttachmentsView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, CurrentY, 640, 300) collectionViewLayout:CollLayout];
//AttachmentsView.backgroundColor = [UIColor whiteColor];
[AttachmentsView registerClass:[Cell class] forCellWithReuseIdentifier:#"ReuseCell"];
[VaribleStore SharedInstance].Attachments = [[NSMutableArray alloc] initWithObjects:[UIColor yellowColor],[UIColor redColor],[UIColor blueColor], nil];
AttachmentsView.delegate = self;
AttachmentsView.dataSource = self;
[self.ScrollView addSubview:AttachmentsView];
[AttachmentsView reloadData];
The key was to use the UICollectionViewFlowLayout, the reason is that UICollectionViewLayout is an abstract class, and can not be used for this purpose. after i made that switch ios called cellForItemAtIndexPath as normal.
You can't use UICollectionViewLayout as is. It's an abstract base class and it has to be subclassed. You can use UICollectionViewFlowLayout for grid-like views if you don't want to write your own.

Showing UIImagePickerController in UIPopoverController with existing UINavigationController (adding back button)

So I have a UIPopoverController what houses my UINavigationController where I have my UITableViewController however one of my options on the UITableView is to go and select an image with the UIImagePickerController... Now on the iPhone I can simply use presentModalViewController:animated: however calling that from within a UIPopoverController causes a crash so thats not possible...
I also know the UIImagePickerController needs its own UINavigationController so I can't just push pushViewController:animated: either...
So I figured out that if I keep a link to the UIPopoverController I created, I can then use setContentViewController:animated: to switch to the UIImagePickerController's viewController...
However, I am now stuck at giving the user a way to go back to the previous UINavigationController as I need to be able to add a cancel button to the UIImagePickerController but when I try to do this the cancel button won't get added...
Heres my code that i'm using
-(void)doPhotoalbums {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imagePicker setContentSizeForViewInPopover:CGSizeMake(320, 480)];
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:nil];
[imagePicker.navigationItem setLeftBarButtonItem:cancel];
//[self presentModalViewController:imagePicker animated:YES];
[[self parentPopoverController] setContentViewController:imagePicker animated:YES];
} else {
[UIAlertView showMessage:#"This device does not have any photo albums."];
}
}
So my question is.. Does anybody know how I can get around this? either by adding a cancel/back button what I can hook up to make the navigationControllers switch back or another way to present this (i'd like to avoid switching between two UIPopoverControllers but I don't know what else I can do..
Thanks
Liam
Ahh.. after a little break I found this: https://discussions.apple.com/thread/1710435?start=0&tstart=0
using the UINavigationControllerDelegate you can use the navigationController:willShowViewController:animated: method to access the navigationBar.. then with some code (below) you can add a button.
if ([navigationController isKindOfClass:[UIImagePickerController class]]) {
UINavigationBar *bar = navigationController.navigationBar;
UINavigationItem *top = bar.topItem;
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(imagePickerControllerDidCancel:)];
[top setLeftBarButtonItem:cancel];
} else {
//do non imagePickerController things
}

objective-c init UITableViewController with data array?

I have four buttons, which have to push the same uiTableViewController, with different data in every case. I think I need to implement an initializer for my table and pass a NSArray. and then I'll check which button is pushed with the sender property.
Is that ok? How can I implement this initializer? I searched in the doc but I haven't get solution yet.
in your view controller you can initialize tableview by [UITableView alloc] initWithFrame:self.view.bounds]; and put this initialization in viewdidload method of your view controller,you don't need xib now.
to be specific use
[self.tableView = [UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.000];
self.tableView.bounces=YES;
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;

Overriding -handlePan: in UIScrollView

Is it ok to override -handlePan: in a UIScrollView subclass?
i.e. my app won't get rejected from the app store?
Thanks for sharing your views.
Edit: what about calling -handlePan: in another method of my subclass?
In case anyone is interested, what I did instead of overriding was disabling the default UIPanGestureRecognizer and adding another instance of UIPanGestureRecognizer which is mapped to my custom handler.
Edit for twerdster:
I did it like this
//disables the built-in pan gesture
for (UIGestureRecognizer *gesture in scrollView.gestureRecognizers){
if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]){
gesture.enabled = NO;
}
}
//add your own
UIPanGestureRecognizer *myPan = [[UIPanGestureRecognizer alloc] init...];
//customize myPan here
[scrollView addGestureRecognizer:myPan];
[myPan release];
You can make the code even shorter.
//disables the built-in pan gesture
scrollView.panGestureRecognizer.enabled = NO;
//add your own
UIPanGestureRecognizer *myPan = [[UIPanGestureRecognizer alloc] init...];
[scrollView addGestureRecognizer:myPan];
[myPan release];