objective-c init UITableViewController with data array? - objective-c

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;

Related

How to create a UIAlertView with table view

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

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.

Can I Show A SplitViewController Using presentModalViewController?

I don't know if what I'm trying to do is possible, but because I haven't the desired results, I guess not.
What I'm trying and need to do is to call a SplitViewController from a previous ViewController, using presentViewController.
I know, SplitViewController have to be the rootViewController, but I need to explore the most possible options to achieve what I need to do.
I have a MainMenu with buttons, and with every button, I need to call a SplitViewController. First, how can do this?
What I'm trying to do is this:
First, in AppDelegate I'm calling the MainMenu, and add as a subview and other things:
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:self.mainMenu.view];
[self.mainMenu presentModalViewController:self.firstMenu animated:NO];
[self.window makeKeyAndVisible];
return YES;
}
Then, in the MainMenu, I'm calling SecondViewController, in modal view, using this code:
SecondViewController *secV = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
secV.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:secV animated:YES];
In this SecondViewController, I'm creating SplitViewController, with Master & DetailViewController's, using this code:
-(void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UISplitViewController *splitViewController = [[UISplitViewController alloc]init];
SecondMenuViewController *secMenu = [[SecondMenuViewController alloc]init];
UINavigationController *navLef = [[UINavigationController alloc]init];
[navLef pushViewController:secMenu animated:NO];
SecondMainViewController *secMain = [[SecondMainViewController alloc]init];
UINavigationController *navRig = [[UINavigationController alloc]init];
[navRig pushViewController:secMain animated:NO];
splitViewController.delegate = secMain;
splitViewController.viewControllers = [NSArray arrayWithObjects:navLef, navRig, nil];
MainAppDelegate *mainApp = [[MainAppDelegate alloc]init];
[mainApp changeRootViewController:splitViewController];
navRig = nil;
navLef = nil;
secMain = nil;
secMenu = nil;
splitViewController = nil;
}
As you can see, I'm calling a method in MainAppDelegate, to change view and RootViewController, because SplitViewController have to be RootViewController. This is the method:
-(void)changeRootViewController:(UISplitViewController *)splitViewController{
[self.window addSubview:splitViewController.view];
self.window.rootViewController = splitViewController;
}
I know, this looks like a mess. And when I run, the SplitViewController never shows, so I assume, what I'm trying to do is not possible? Or In what I'm wrong?
If it is everything, what can I do to show a SplitViewController after my MainViewController?
I'm using XCode4.4 and iOS5
Thank you very much
A better way would be to make your UISplitViewController the root view controller in application:didFinishLaunchingWithOptions:. Then present your MainMenu on top of it. You can change the subviews displayed by the split view controller to correspond to what button the user pushes in your MainMenu.
First, didFinishLaunchingWithOptions: is too early to be calling presentModalViewController. You haven't even got an interface yet!
Second, you don't seem to have a root view controller (although perhaps you're getting one from a nib? you should probably stop doing that; use the techniques shown in the current application templates).
Third, note that now that we have custom container views, there is no need for you to use UISplitViewController at all; you can construct your own view / view controller hierarchy, and you might be happier doing so, since UISplitViewController is not a very well-constructed class.

Image not being set in method

I have a class with a viewDidLoad method and an artworkInfo method as follows:
-(void)viewDidLoad {
mainDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
[super viewDidLoad];
}
-(void)artworkInfo:(NSNumber *)pos{
mainDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
[self.image setImage:(UIImage *)[[mainDelegate.mapAnnotations objectAtIndex:0]image]];
}
the mainDelegate thing is to gain access to the appDelegate where an array is stored, but anyway, with the "[self.image setImage...]" command where it is, the image on the app does not appear, but when I copy that exact line of code into the viewDidLoad method, it shows up like it should. I know that the artworkInfo method is being called because I debugged it and it goes through, so I can't figure out why the command would not be doing anything it's current method while it will in the viewDidLoad...?
Also, here is where the method is called and this new view is loaded from another class:
infoPage *info = [[infoPage alloc] initWithNibName:#"infoPage" bundle:nil];
info.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:info animated:YES];
infoPage *myInfoPage = [[infoPage alloc] init];
[myInfoPage artworkInfo:position];
[info release];
OH, I see the problem. You're instantiating 2 different infoPage classes.
Change this:
infoPage *info = [[infoPage alloc] initWithNibName:#"infoPage" bundle:nil];
info.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:info animated:YES];
infoPage *myInfoPage = [[infoPage alloc] init];
[myInfoPage artworkInfo:position];
[info release];
to this:
infoPage *info = [[infoPage alloc] initWithNibName:#"infoPage" bundle:nil];
info.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:info animated:YES];
[info artworkInfo:position];
[info release];
Ok detailed answer. In order to understand why this image is not displaying properly you have to first look at how Runloops work in Objective C.
While viewDidLoad is the method that is called when a view is loaded and it is technically also called before a view is displayed and it's view objects initialized. Since presentModalViewController is an animation there is actually some threading going on in the works.
viewDidLoad gets called before the animation is created for the presentModalView. This initializes your objects. However, due to some of the inner workings of UI Kit some processes are loaded off into a thread. When they complete they run callback methods on the main UI thread.
Since presentModalViewController is a non-blocking method your artworkInfo method gets added to the mainRunLoop before the initializer form thread adds its callback methods to the main run loop. The best approach would be to have both a UIImage property of your viewController and a UIImageView.
set the value of UIImage by calling artworkInfo BEFORE the presentModalViewController method.
in your ViewDidLoad go ahead and set the value of your UIImageView
[self.imageView setImage:self.image];
Problem solved.
This seems pretty straight forward.
So you initialize your nib and try to call your method artwork before the nib is fully loaded. <-- This is not working for you.
Then you do additional initialization by overrider viewDidLoad per the doco where the nib is loaded <-- This is working for you
So the answer is, when you call setImage before your nib is loaded, then there is nothing to set the image to. When you call setImage in viewDidLoad your nib is loaded and then things should work just fine.
I hope this explains it a bit.

Animate loaded subview

How would I make this code animate in the SplashView NIB instead of just making it appear (e.g. the UIModalTransitionStyleFlipHorizontal style)? I am using a UITabBarController type project.
- (IBAction)showSplash:(id)sender {
// Hide toolbar
self.tabBarController.tabBar.hidden = YES;
// Splash
[[NSBundle mainBundle] loadNibNamed: #"SplashView" owner: self options: nil];
[self.view addSubview: splashView];
[window makeKeyAndVisible];
}
Bit hard to tell your context with this small bit of code. Basically, if you want to push a viewController modally, in your -(IBAction)showSplash method (you don't need to send the sender if you're not using it, BTW), I would use some code similar to this:
SplashViewController *svc = [[SplashViewController alloc] init]; (assuming nib is same name)
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:svc animated:YES];
[svc release];
Then in your SplashViewController you would have an IBAction that calls:
[self dismissModalViewController animated:YES];
You don't actually have to hide the tabBar when you are presenting a modalViewController. It won't be there. The idea of a modalViewController is that it blocks all user interaction with the app except for the modal view, until it is dealt with.
Hope this helps.