How to call a UIViewController from a UIView SubClass? - ios

I have main UIViewController in which I have an UITableView.
In the cell of UItableView I had already add an UICollectionView.
in -
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
I am calling an method of main UIViewController.
UIViewController *view = [[UIViewController alloc] init];
[view my_method];
in main view.
-(void)my_method {
AnotherName *view_my=[[AnotherName alloc]initWithNibName:#"AnotherName" bundle:nil];
[self.navigationController pushViewController:view_my animated:NO];
}
Viewdidload method is calling but view is not showing.

It is because you initialise by calling [[UIViewController alloc] init]. This always returns empty view.
You should instead call this
If you use storyboard give the view controller a storyboard id and call this
MainViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"storyboard id"];
If you use interface builder, call this
MainViewController *controller = [[MainViewController alloc] initWithNibName:#"Nib File Name" bundle:nil];

i got solution..
AnotherName *view_my = [[AnotherName alloc]initWithNibName:#"AnotherName" bundle:nil];
self.window.rootViewController = view_my;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
Spacial thanks to #The Mech System .. To help me out find the solution ..
now it is working fine.

Related

CollectionViewCell clicked Slow pass to another View controller

I am developing iOS App. When I click on CollectionView Cell to pass the value to Another ViewController. Very Slow Pass to One ViewController to Another View Controller.
MEObject *objects = [_arrayData objectAtIndex:indexPath.row];
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
MEDetailPageViewController *view = [mainStoryboard instantiateViewControllerWithIdentifier:#"detailPage"];
[view setProduct_ID:objects.me_product_id];
window.rootViewController = view;
In your collectionview didselectitematindexpath method change code as following:
MEObject *objects = [_arrayData objectAtIndex:indexPath.row];
MEDetailPageViewController *view = [self.Storyboard instantiateViewControllerWithIdentifier:#"detailPage"];
[view setProduct_ID:objects.me_product_id];
[self.navigationController pushViewController:view animated:YES];

Embed navigation controller in modal view contriller programatically

I'm presenting a modal view controller using following code,
[[mInfo objectForKey: kNavigationController] presentViewController:(UIViewController*)modalViewControlr animated:YES completion:nil];
UITableView. On selecting a table view cell I want to navigate it to another UIView called navigatedView.
Is this can be done by embedding self (i.e,modalViewControlr) in navigation Controller and add view (i.e, navigatedView) to a view controller and present it?
for example,
// in modalViewControlr
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:self];
UIViewController *vc = [[UIViewController alloc]init];
[vc.view addSubview:self.navigatedView];
self.navigatedView.frame=vc.view.frame;
[passcodeNavigationController pushViewController: vc animated:YES];
//
}
Please help....
I prefer do it in this way which is easier to understand.
1.Embed the navigationController in AppDelegate:
ModalViewControlr *vc = [[ModalViewControlr alloc]init];
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = passcodeNavigationController;
2.Create a new class NavigatedViewControllerand custom the controller in viewDidLoad
Create an UIView call navigatedView
#property (nonatomic, strong) UIView *navigatedView;
-(void)viewDidLoad
{
[super viewDidLoad];
_navigatedView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.navigatedView.backgroundColor = [UIColor redColor];
[self.view addSubView:self.navigatedView];
}
3.Then return to modalViewControlr where you want to push from.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NavigatedViewController *vc = [[NavigatedViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}

Xcode 5 Storyboard - presentViewController not presenting my existing Viewcontroller and giving black screen

I have two view controller named ViewController and SecondViewController. From the first view controller i m trying to call my SecondViewController after 5 second. but it will giving me black screen instead of my expected view.
Here is my view controller code,
`
#import "ViewController.h"
#import "SecondViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(loadingNextView) withObject:nil afterDelay:5.0f];
}
-(void)loadingNextView{
SecondViewController *SVC = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
SVC.view.backgroundColor = [UIColor redColor];
[self presentViewController:SVC animated:YES completion:NULL];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end`
Please suggest. If i used button for navigation then its work but my needs is to call automatically on timer.
If your SecondViewController is in storyboard instead of alloc init you have use instantiateViewControllerWithIdentifier
[self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
Dont forget to give the identifier for the controller in the storyboard. So the method will be
-(void)loadingNextView{
[self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
SVC.view.backgroundColor = [UIColor redColor];
[self presentViewController:SVC animated:YES completion:NULL];
}
If you are using storyboard then you cannot initialise the controller with the nib Method "initWithNibName" basically searches for an Xib file, which you do not have as you are using a storyboard, better initialise it in the following way ..
-(void)callNextView{
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"yourStoryBoarddName" bundle:nil];
yourViewController *VC = [storyboard instantiateViewControllerWithIdentifier:identifier];
[self presentViewController:VC animated:YES completion:NULL];
}
If you are not using the storyboard, you can simply init the new controller like this
SecondViewController *SVC = [[SecondViewController alloc]init];
[SVC setBackgroundColor: [UIColor redColor]];
[self presentViewController: SVC animated: YES completion: nil];
You need to initialize the controller exact from the storyboard.
- (void)loadingNetView
{
UIStoryboard storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
NSString *identifier = #"SecondController"; // you need to set identifier in storyboard
SecondViewController *controller = [storyboard instantiateViewControllerWithIdentifier:identifier];
}

navigationController = null in TableViewController's pushViewController method (popover)

First of all, sorry for my English.
I'm trying to do an app for iPad with a navigation controller which pushes view controllers when a button "Next" is selected. But I also want to have a popover, called from a button in the nav bar, that allows the user to "jump" from one view controller to another, pushing it with the tableView:didSelectRowAtIndexPath: and the pushViewController:animated: methods, but it's not working.
Summary:
Tab bar -> switches between FirstViewController and SecondViewController (works just fine)
Nav bar (button Next) -> switches between SecondViewController, FirstSlideController and SecondSlideController (it's also good)
Popover -> user selects SecondViewController, FirstSlideController or SecondSlideController (here's the problem!)
Codes:
AppDelegate
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[navigationController1, navigationController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
TableViewController(popover)'s didSelectRowAtIndexPath method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0){
FirstSlideController *detailViewController = [[FirstSlideController alloc] initWithNibName:#"FirstSlideController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
else if(indexPath.row == 1){
SecondSlideController *detailViewController = [[SecondSlideController alloc] initWithNibName:#"SecondSlideController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
else{
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
}
SecondViewController (with delegate suggested by maros)
-(void) showPopover:(id) sender
{
TableViewController *PopoverView = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
self.popOver = [[UIPopoverController alloc] initWithContentViewController:PopoverView];
self.popOver.delegate = self;
[self.popOver presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedArrowDirections: UIPopoverArrowDirectionUp animated: YES];
}
I tried to print self.navigationController and it says it's null. I'd appreciate any help.
Thank you.
Presnented UIPopoverController is not pushed on the navigation stack in the view controller from which it was presented. It is a separate view controller.
Therefore the navigationController inside the popover is nil.
What I would recommend you is to create a delegate MyNavigationPopoverDelegate (the class that creates a popover (PopoverController). Pass it's instance as an delegate to the TableViewController.
After users clicks on some button inside popover, call delegate's method to processes button clicks (myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex).
Then maybe dismiss delegate?
and finally change navigation however you want! :)
#protocol MyNavigationPopoverDelegate
- (void) myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex;
#end
#interface TableViewController : UITableVieController // your viewController in popover
... // your code
#property (nonatomic, weak) NSObject <MyNavigationPopoverDelegate> * delegate;
... // your code
#end
#implementation TableViewController
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.delegate myNavigationPopover:self clickedButtonAtIndex:indexPath.row];
}
...
#end
// defines that SecondViewController implements the delegate's method
#interface SecondViewController <MyNavigationPopoverDelegate> : UIViewController
// your code
#end
// code where you presenting popover
#implementation SecondViewController
// This is the method that is executed after your button press and it is responsible for presenting a popover
- (void) presentPopover{
...
myPopover.delegate = self; // setting the delegate
[myPopover presentPopoverFromXXX ...]; // however you present it
...
}
- (void) myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex
{
UINavigationController *currentNavigationController = ; // get the navigation controller from the tab bar
if(buttonIndex == 0){
FirstSlideController *detailViewController = [[FirstSlideController alloc] initWithNibName:#"FirstSlideController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
}
else if(buttonIndex == 1){
SecondSlideController *detailViewController = [[SecondSlideController alloc] initWithNibName:#"SecondSlideController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
}
else{
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
}
}
#end;

iOS table navcontroller not working

I have a table:
- (void)viewWillAppear:(BOOL)animated
{
self.searchBar.delegate=self;
self.searchResultTable.delegate=self;
self.searchResultTable.dataSource=self;
[self.searchResultTable setHidden:YES];
}
In my cells I insert text and when I click on cell I need go to another table but this not work:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewController *tvc=[[TableViewController alloc]initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:tvc animated:YES];
}
Assuming you are doing something like this to present this controller:
SearchViewController *searchViewController = [SearchViewController alloc] init];
[self presentModalViewController:searchViewController animated:YES];
then your view controller doesn't have a navigation controller
You need to change it to
SearchViewController *searchViewController = [SearchViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:searchViewController];
[self presentModalViewController:navController animated:YES];
then [self.navigationController pushViewController:tvc animated:YES]; will work

Resources