I am trying to create an array of views that I can then swipe though in my applicant.
I have tried to set up my array in my view did load then load the first view in the array into view.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = #"Prototype";
//Initalizse the swipe gestuer listener
[self setupLeftSwipeGestureRecognizer];
[self setupRightSwipeGestureRecognizer];
//alloc and init viewcontroller nibs
self.detailViewA = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
self.detailViewB = [[DetailViewControllerB alloc]initWithNibName:#"DetailViewControllerB" bundle:[NSBundle mainBundle]];
self.detailViewC = [[DetailViewControllerC alloc]initWithNibName:#"DetailViewControllerC" bundle:[NSBundle mainBundle]];
// Create Array
viewArray = [NSArray arrayWithObjects:detailViewA_, detailViewB_, detailViewC_, nil];
// set detail View as first view
[self.view addSubview:[viewArray objectAtIndex:0]];
}
After which it crashes. and produces this message.
2012-05-31 10:06:33.769 SMPrototypeB[2394:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DetailViewController superview]: unrecognized selector sent to instance 0x131b00'
You are adding a view controller as a subview but what you need to do is add the view of that viewController.
UIViewController *tempVC = (UIViewController*)[viewArray objectAtIndex:0];
[self.view addSubView:tempVC.view];
Related
I am working on a app, which uses UIPopoverController, I am getting a problem in presenting that popover, I have a UIView which is added on self.view, and a tableview added on that view, that table view has a custom cell, which includes a UITextField, on didBeganEditing method I need popover to open.
Here is the code:
table = [[UITableView alloc]initWithFrame:CGRectMake(textField.frame.origin.x,textField.frame.origin.y,200,100) style:UITableViewStylePlain];
table.tag=3;
[table setDataSource:self];
[table setDelegate:self];
[table reloadData];
UITableViewController *tableViewController= [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
tableViewController.tableView = table;
popOver= [[UIPopoverController alloc]
initWithContentViewController:tableViewController];
[popOver presentPopoverFromRect:popRect inView:cell permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
I will getting following exception
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'
here you present popoverview in the cell and give the Rect to popover of UITableViewController and so you get this error.. here it have not view hierarchy so its not get window and crashed here just follow the view hierarchy.
I have a UIButton in UIVIewController and I need to push it to the UIVIew when I press the button.But this gives me the warning of incompatible pointer type sending
how to do this
What I am doing this:
-(void)press{
displayView *disp=[[displayView alloc]init];
[self presentModalViewController:disp animated:No];
}
this gives me warning and crashes my application.
presentModalViewController accepts a UIViewController instance (not UIView). If you want to display a particular view, place it within a view controller first.
UIViewController *viewController = [[UIViewController alloc] init];
DisplayView *displayView = [[DisplayView alloc] init];
[viewController.view addSubview: displayView];
[self presentModalViewController:viewController animated:NO];
I'm using Storyboards in xcode with iOS5. I have a TabBarController with 6 tabs. Prior to the TabController a user selects a type of account A oR B, if type B is selected I would like to hide one of the tabs.
I have a subclass of UITabBarController and this piece of code works but its not quite what I want.
if (accountType == 2) {
[[[[self tabBar] items] objectAtIndex:1] setEnabled:NO];
}
This makes my second tab dark and unusable which is ok, but I really wanted this to work...
[[[[self tabBar] items] objectAtIndex:1] setHidden:YES];
But it causes this error: -[UITabBarItem setHidden:]: unrecognized selector sent to instance 0x856f490
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarItem setHidden:]: unrecognized selector sent to instance 0x856f490'
Is there another way of achieving this?
Why not waiting with the initialization of the tabBar viewControllers until you know which type of Account your user selects? To do so use the setViewControllers:animated:method for e.g. as followed:
if (accountType == 1) {
NSArray* controllersForTabBar = [NSArray arrayWithObjects:myVC1,myVC2,myVC3,myVC4,myVC5,myVC6 nil];
[[[self tabBar] setViewControllers:controllersForTabBar] animated:YES];
}
if (accountType == 2) {
NSArray* controllersForTabBar = [NSArray arrayWithObjects:myVC1,myVC2,myVC3,myVC4,myVC5, nil];
[[[self tabBar] setViewControllers:controllersForTabBar] animated:YES];
}
The apple doc for this method says:
When you assign a new set of view controllers runtime, the tab bar
controller removes all of the old view controllers before installing
the new ones. When changing the view controllers, the tab bar
controller remembers the view controller object that was previously
selected and attempts to reselect it. If the selected view controller
is no longer present, it attempts to select the view controller at the
same index in the array as the previous selection. If that index is
invalid, it selects the view controller at index 0.
Regarding your error message: You get that error because the tabBar doesn't implement a method setHidden:.
d.ennis answer pointed me in the right direction. Have to tweak it slightly for ios5 with Storyboards...
// load the storyboard by name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
if (accountType == 1) {
UIViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:#"First"];
UIViewController *svc = [storyboard instantiateViewControllerWithIdentifier:#"Second"];
} else {
UIViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:#"First"];
UIViewController *svc = [storyboard instantiateViewControllerWithIdentifier:#"Second"];
UIViewController *tvc = [storyboard instantiateViewControllerWithIdentifier:#"Third"];
}
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
NSArray *controllersForTabBar = [NSArray arrayWithObjects: fvc, svc, nil];
[tabBarController setViewControllers:controllersForTabBar animated:NO];
[self.view addSubview:tabBarController.view];
I've got a stack scroll view app (like the Twitter and Facebook apps) using PSStackedView
It's creates the view with this stack:
AppDelegate
// set root controller as stack controller
MenuRootController *menuController = [[MenuRootController alloc] init];
self.stackController = [[PSStackedViewController alloc] initWithRootViewController:menuController];
self.window.rootViewController = self.stackController;
[self.window makeKeyAndVisible];
Root nav controller has a UItable, a cell touch loads the next view
// Load Home Stories table
PSStackedViewController *stackController = XAppDelegate.stackController;
UIViewController*viewController = nil;
while ([stackController.viewControllers count]) {
//NSLog(#"launchStories");
[stackController popViewControllerAnimated:YES];
}
viewController = [[TestView alloc] initWithNibName:#"TestView" bundle:nil];
((TestView *)viewController).indexNumber = [stackController.viewControllers count];
viewController.view.width = roundf((self.view.width - stackController.leftInset)/2);
if (viewController) {
[XAppDelegate.stackController pushViewController:viewController fromViewController:nil animated:YES];
}
In this view I want to use the MBProgressHUD (https://github.com/matej/MBProgressHUD/) to display a nice loading XML message
HUD = [[MBProgressHUD alloc] initWithView:self.view.window];
[self.view.window addSubview:HUD];
HUD.delegate = self ;
HUD.labelText = #"Loading";
[HUD showWhileExecuting:#selector(myTask) onTarget:self withObject:nil animated:YES];
But
HUD.delegate = self ;
Throws an warning and the app crashes
Assigning to 'id<MBProgressHUDDelegate>' from incompatible type 'TestView *'
I've tried all sorts of combinations to try and find the current controller but to no avail, I can find the width of the current controller for instance with
PSStackedViewController *stackController = XAppDelegate.stackController;
NSLog(#"%f",stackController.view.width);
which prints 748.000000. But I can't find work out what 'self' should be.
Any ideas?
TestView needs to implement the MBProgressHUDDelegate protocol. In TestView.h make it look something like this:
#interface TestView : ClassYouInheritFrom <MBProgressHUDDelegate>
I've got a very stupid problem. I have a TableView where I'd like to make each cell editable by pushing into a DetailController. It's working so far, but when I do
NSLog(#"selectedItem: %#", selectedItem_);
in viewDidLoad, the log displays:
selectedItem: (null)
selectedItem: Toe
RootViewController (didSelectRowAtIndexPath):
NSString *selectedItem = [content objectAtIndex:indexPath.row];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
detailViewController.selectedItem_ = selectedItem;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
DetailViewController (viewDidLoad):
NSLog(#"selectedItem: %#", selectedItem_);
[super viewDidLoad];
What am I doing wrong?
you cannot assume that viewDidLoad is called only once. it may be called multiple times if the view is unloaded because of memory, or when the view is getting loaded from the nib.
put the initialization in your init method.
similar question and answer on stackoverflow
viewDidLoad getting called twice on rootViewController at launch