Close UIViewController - objective-c

I'm trying to close soon after a UIViewController call another. It should be simple, but I'm not succeeding. I am using the following method:
- (IBAction)bClose:(id)sender {
iTest *test = [[iTest alloc] initWithNibName:#"iTest" bundle:[NSBundle mainBundle]];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:test animated:YES];
[test release];
[self dismissModalViewControllerAnimated:YES];
}

I believe it's not simple. I have done something similar but instead I present the second in the first, then dismiss the first when the second wants to dismiss (dismissing both at the same time). To explain what I mean better:
main -> present controller1
controller1 -> present controller2
controller2 -> dismiss controller1
I'm sure there is a better solution to it though.
Something like this:
inside first controller:
UIViewController *c1 = [[UIViewController alloc] init];
[self presentModalViewController: c1 animated:YES];
inside c1:
UIViewController *c2 = [[UIViewController alloc] init];
c2.c1 = self;
[self presentModalViewController: c2 animated:YES];
inside c2:
[c1 dismissModalViewControllerAnimated:YES];

i also had to try something similar..but ran into much of the same problem..
My solution was to initiate a timer with time 0.5 sec and then dismiss the view in the selector
Worked for me.

Related

xcode 9: presentViewController removing all previous controller

I have view controller and implement like this :
loginViewController = [[LoginViewController alloc] init];
loginViewController.delegate = self;
[self.view addSubview:loginViewController.view];
[self addChildViewController:loginViewController];
[loginViewController didMoveToParentViewController:self];
Inside loginViewController, I want to show another view controller but I implement like this :
otherViewController = [[OtherViewController alloc] initWithNibName:nil bundle:nil];
otherViewController.delegate = self;
[self presentViewController:otherViewController animated:YES completion:nil];
Everytime I done with otherViewController and want to dismiss the view controller [self dismissViewControllerAnimated:YES completion:nil];
it restarted from the beginning and not showing the loginViewController. I tried to check using Debug View Hierarchy and got that no loginViewController is implemented when I called presentViewController:otherViewController
Why is this happened ?

Creating multiple subviews inside view controller

I have a view controller named ViewController.
ViewController displays a UIView that has a button on it, which allows me to advance to a second view controller - SecondViewController.
SecondViewController also has a button on it, which allows me to advance to a third view controller.
However, I am having trouble displaying ThirdViewController. When I tap the button in SecondViewController it throws an error:
Warning: Attempt to present ... on ... whose view is not in the window hierarchy!
I have been to a bunch of other sites and posts that address this issue, but cannot seem to find a working solution. I have implemented quite a few solutions, but none seem to work.
Here is my code:
- (void)secondaryView:(UIView *)secondaryView
{
UIView *theView = secondaryView;
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.frame = [UIScreen mainScreen].bounds;
[viewController.view addSubview:theView];
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:viewController animated:YES completion:nil];
}
secondaryView is a UIView that I am constructing elsewhere in the application. I add it to the viewController then present the viewController.
Is there any way to dynamically create UIViewControllers, add a UIView to each one, and add them to the window hierarchy?
If you can use navigation in your ViewControllers, you can try the below code
In the place where you call the firstViewController,
-(void)callFirst
{
FirstViewController *first = [FirstViewController alloc] init];
UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:first];
navigationController.modalPresentaionStyle = UIModalPresenationFormSheet;
[self presentModalViewController:navigationController animated:YES];
}
And in the FirstViewController button action ,you can write
-(void)callSec
{
SecondViewController *sec = [SecondViewController alloc] init];
[self.NavigationController pushViewController:sec animated:YES];
}
And in SecondViewController you can do the same
-(void)callThird
{
ThirdViewController *third = [ThirdViewController alloc] init];
[self.NavigationController pushViewController:third animated:YES];
}
Try this...And in these ViewControllers you can do whatever coding you want to meet the requirement like
-(void)viewDidLoad
{
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(20,20,400,400)];
newView.backGroundColor = [UIColor redColor];
[self.view addSubView:newView];
}

Instance variables not working in ViewController in UINavigationController

I'm still new to iOS development but I've ran into a problem that I can't solve and I've tried looking online but can't find anything yet.
I'm using a UIImagePickerController to pick and image and I'm using it in the App Delegate. When an image is returned, in the imagePickerController:didFinishPickingMediaWithInfo method, I want to make a new navigation controller with a view controller and put it over the "app".
Here is how I'm doing it:
CustomNavigationController *customNavigationController = [[CustomNavigationController alloc] init];
PhotoViewController *photoViewController = [[PhotoViewController alloc] initWithNibName:#"PhotoViewController" bundle:[NSBundle mainBundle]];
[customNavigationController pushViewController:photoViewController animated:NO];
[customNavigationController.view setFrame:[[UIScreen mainScreen] applicationFrame]];
[photoViewController release];
[self.window addSubview:customNavigationController.view];
//Call method of photoViewController.
[[customNavigationController.viewControllers objectAtIndex:0] addPhotoFromData:info];
[self.tabBarController dismissViewControllerAnimated:YES completion:nil]; //Get rid of UIImagePickerController
However in the photoViewController, I don't have access to any instance variables synthesized and loaded in viewDidLoad. They all return to null. There is also a tableView and calls to reload the tableView do not actually cause the tableView to respond.
Here is some of the code from photoViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.photoCount = 0;
self.timestamp = [[NSDate date] timeIntervalSince1970];
self.photos = [[NSMutableArray alloc] initWithCapacity:5];
self.photosData = [[NSMutableArray alloc] initWithCapacity:5];
self.uploadingCount = 0;
UINib *customCell = [UINib nibWithNibName:#"CustomTableCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:customCell forCellReuseIdentifier:#"customCell"];
self.tableView.separatorColor = [UIColor clearColor];
NSLog(#"%#", self);
}
and also the addPhotoFromData method:
- (void)addPhotoFromData:(NSDictionary *)info
{
[self.photos addObject:info];
NSLog(#"%#", self.photos); //Doesn't add "info" and does not return anything when later called from other methods.
[self.tableView reloadData]; //Doesn't work
Everything was working before I add in the UINavigationController. I'm completely lost.
EDIT: After some more debugging attempts, I have discovered that the view is not loaded when addPhotoFromData is called. viewDidLoad is called afterwords. Is there a way to delay method calls?
Any reason you can't have the PhotoViewController call addPhotoFromData: inside the viewDidLoad method? You can always access properties of the app delegate from the view controller:
YourAppDelegateType * delegate = [UIApplication sharedApplication].delegate;
[self addPhotoFromData:delegate.info];

How do I push many Views from one View using UINavigationController

I code many transition App using UINavigationController.
This is my transition image
"Main"-->"Second"-->"Third"-->"Fourth"
"Second"-->"A"-->"B"
"Second"-->"C"
"Third"-->"D"
"Main" is RootViewController.
At No2 and No3, if (["boo" isEqual:"foo"]){transit "A"} else if ("boo"...){transit "C"}.
I could make "Main" to "Forth", No.1 transition.
like this
ThirdViewController *thirdView = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thirdView animated:YES];
But I can't make "Second" to "A"and "B".
I thought to set UIRootViewController to "Second".
But "Second" needs to transit "A" and "C".
at "Second" like this...
SecondViewController.m
AViewController *aView = [[AViewController alloc] init];
[self.navigationController pushViewController:aView animated:YES];
How can I code?
Thank you! And sorry in poor English.
Please help me!
NSInteger index = 0;
for (UIViewController *view in self.navigationController.viewControllers) {
if([view.nibName isEqualToString:#"RootViewController"])//put any `XIB name` where u want to navigate
break;
index = index + 1;
}
[[self navigationController] popToViewController:[[self.navigationController viewControllers] objectAtIndex:index] animated:YES];
Try this code to push to a controller
DrawImgViewcontroller *ObjImageViewController = [[DrawImgViewcontroller alloc] initWithNibName:#"DrawImgViewcontroller" bundle:nil];
[self.navigationController pushViewController:ObjImageViewController animated:YES];
[ObjImageViewController release];
and use below code to go back
[self.navigationController popViewControllerAnimated:YES];
and use this to any controller you like
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:NO];//Instead of one you can use the currect index you need

Cannot get Twitter-OAuth-iPhone to work

demo works as expected, no problems. But now I'm trying to integrate it into my project. I use no xib-s, code only:
OAuthTwitterDemoViewController *vc = [[OAuthTwitterDemoViewController alloc] init];
[[UIApplication sharedApplication].keyWindow addSubview:vc.view];
[vc release];
it compiles and runs with no errors, but the actual OAuthTwitterDemoViewController is never visible. I've also tried it from a custom viewController with [self.view addSubview:vc.view]
What's the secret??
Update:
OK, here's what I did with NavigationController:
TwitterAuthViewController *vc = [[TwitterAuthViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
[vc release];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
OK, the problems seems to be that I called [self presentModalViewController: controller animated: YES] from ModalViewController->NavigationController. That's where Cocoa touch OR Twitter-OAuth-iPhone (I don't really know which one exactly) have a problem. But this is how I need it.
Solution would be, as Ben already said, simply to push SA_OAuthTwitterController like [self.navigationController pushViewController:controller animated: YES];.
The only remaining problem is that SA_OAuthTwitterController internally creates own NavigationController bar, so that now I have 2 navigation bars visible.