How to dismissing multiple view controllers? - objective-c

I have a storyboard with 3 view controllers: QuestionsTableViewController, QuestionViewController and AnswerViewController
For all intensive purposes, QuestionsTableViewController is basically my main menu. it has a selection of topics which when selected populate a question label in QuestionViewController which is then segued to.
The user enters his/her answer in a UITextField and hits a submit button causing a modal segue to the AnswerViewController which has a label that returns either a correct or incorrect message to the user based on a comparison of their answer to the coded correct answer. This final View Controller also has a button (back to menu) that when clicked should take the user back to the QuestionsTableViewController (i.e. my menu).
This last part (the returning to the menu) is where I am having problems.
I can dismiss the AnswerViewController multiple ways, but I cant figure out what I need to do to dismiss the QuestionViewController as part of this same button press.
I am including snipets of my QuestionViewController and AnswerViewController classes below.
QuestionViewController.m
#import "QuestionViewController.h"
#import "AnswerViewController.h"
#interface QuestionViewController ()
#end
#implementation QuestionViewController
#synthesize currentQuestionDisplay;
#synthesize userAnswerTextField;
#synthesize currentQuestion;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
AnswerViewController *avc = [segue destinationViewController];
[avc setCurrentQuestion:currentQuestion];
[avc setUserAnswer:[userAnswerTextField text]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.currentQuestionDisplay setText:[currentQuestion question]];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setCurrentQuestionDisplay:nil];
[self setUserAnswerTextField:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)dismissKeyboard:(id)sender {
[userAnswerTextField resignFirstResponder];
}
- (void)dimissThisVC
{
[self dismissViewControllerAnimated:YES completion:^(void){}];
}
#end
AnswerViewController.m
#import "AnswerViewController.h"
#import "QuestionViewController.h"
#interface AnswerViewController ()
#end
#implementation AnswerViewController
#synthesize displayCurrentAnswer;
#synthesize currentQuestion;
#synthesize userAnswer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if([userAnswer isEqualToString:currentQuestion.answer]) {
[self.displayCurrentAnswer setText:#"You are correct!"];
}
else {
[self.displayCurrentAnswer setText:#"You are wrong!"];
}
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setDisplayCurrentAnswer:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)dismissAnswerVC:(id)sender {
[[self presentingViewController]
dismissViewControllerAnimated:YES
completion:^(void){ }];
}
#end

When you dismiss the modal view, in the completion block have this code:
[self.navigationController popViewControllerAnimated:YES];
Or if you want to go to the top most controller:
[self.navigationController popToRootViewControllerAnimated:YES];

in an effort to complete this post for others who may reference it, i am reposting user523234's answer here:
In the prepareForSegue method, you missed this line:
avc.delegate = self;
hopefully this can help someone else who has fallen into the same hole that i was in.

Related

Auto Rotation iOS 8 Navigation Controller

I have certain viewControllers which are managed by a UINavigationController (push and pop). I want to restrict different viewControllers to different orientations like the first one should be only in Portrait, second in portrait, third in landscape and fourth can be portrait and landscape both. I set a ViewController on isInitialViewController from storyBoard, the
- (BOOL) shouldAutorotate{
return NO;
}
worked without any problem but when i set the navigation controller (managing these four views by push and pop) as isInitialViewController from storyBoard, this function stopped being called and now autoratates. How can I stop autorotating these views using this UINavigationController as the isInitialViewController. I use the following functions depends on which ViewController it is
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIDeviceOrientationPortrait);//choose portrait or landscape}
- (BOOL) shouldAutorotate{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
//return UIInterfaceOrientationMaskLandscape;
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
// return UIInterfaceOrientationLandscapeLeft |
// UIInterfaceOrientationLandscapeRight;
return UIInterfaceOrientationPortrait;
}
Just subclass UINavigationController and override appropriate methods:
.h File:
#interface CustomUINavigationController : UINavigationController
#property BOOL canRotate;
#end
.m File:
#implementation CustomUINavigationController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
return self.canRotate;
}
#end
What if you make Objective C category for UINavigation Controller and Override interface orientation methods...I think u try to control the Autorotation.
Objective C category
http://rypress.com/tutorials/objective-c/categories

fixed orientation for some view controllers

In my app i've both tabbar and navigationBar. rootview controller tabbar and tabbar has 4 navigation controller.
I want to make some viewcontrollers to be portrait only. It may be a common question but I've tried enough but I could not solve this.
How to make portrait orientation for some view Controller?
I've solved this problem and answering so that if someone face same problem they can get a help.
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
The above methods don't get called of a viewcontroller if they are inside any tabbarcontroller of navigationcontroller. If these methods are declared inside tabbarcontroller or navigation controller then they will get called. In my case the viewcontrollers was inside navigationcontroller and the navigation controllers are inside a tabbarcontroller.
For solving this I make a class FixedOrientationTab, it is a subclass of UITabBarController, and a navigation class OrientationEnabledNavigation, it is a subclass of UINavigationController. Then I implemented shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation methods inside FixedOrientationTab and OrientationEnabledNavigation.
OrientationEnabledNavigation.h
#import <UIKit/UIKit.h>
#interface OrientationEnabledNavigation : UINavigationController
#end
OrientationEnabledNavigation.m
#import "OrientationEnabledNavigation.h"
#interface OrientationEnabledNavigation ()
#end
#implementation OrientationEnabledNavigation
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
FixedOrientationTab.h
#import <UIKit/UIKit.h>
#interface FixedOrientationTab : UITabBarController
#end
FixedOrientationTab.m
#import "FixedOrientationTab.h"
#interface FixedOrientationTab ()
#end
#implementation FixedOrientationTab
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Then if you want to use navigation controller in your project then use OrientationEnabledNavigation and for tabbar FixedOrientationTab. After that if you implement shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation these method inside your viewcontroller then they will get called.
Hope this helps.. :)

Why isn't my UIScrollView scrolling fully?

I dont know why my UIScroll view doesn't work. I am just learning iOS development and when I covered UIScrollView, I had problems in scrolling.
I created a new project just to check the scroll, but I had no luck. I'm using the iOS 6.1 simulator with Xcode 4.6.2.
This is my .h file:
#import <UIKit/UIKit.h>
#interface CDViewController12 : UIViewController<UIScrollViewDelegate>{
IBOutlet UIScrollView *scroll;
}
#end
and my .m file
#import "CDViewController12.h"
#interface CDViewController12 ()
#end
#implementation CDViewController12
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[scroll setScrollEnabled:YES];
scroll.contentSize = CGSizeMake(250, 2000);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[scroll release];
[super dealloc];
}
#end
What am I doing wrong?
Try deactivating the auto layout property.
In the file inspector uncheck the box "Use AutoLayout"
hope this help!
Make sure the the size of the scroll view is not larger than the size of the parent view.

Remove Edit Button from moreNavigationController dont work

i get some problem by removing the Edit Button from the moreNavigationController.
I cant find the mistake, it must be a simple one.
I create some TabBarViewController connect it in the IB with my TabBarViewController.
Here is the Code:
TabBarViewController.h:
#import <UIKit/UIKit.h>
#interface TabBarViewController : UITabBarController <UINavigationControllerDelegate, UITabBarControllerDelegate>
#end
TabBarViewController.m:
#import "TabBarViewController.h"
#interface TabBarViewController ()
#end
#implementation TabBarViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.delegate = self;
[self.moreNavigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
[self.moreNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbar.bg.png"] forBarMetrics:UIBarMetricsDefault];
[self.moreNavigationController.navigationBar.topItem setRightBarButtonItem:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The Edit Button is stil there :/
You need to set the navigationController delegate to your tabBarController. Add the following line in your viewDidLoad method of your TabBarViewController class
self.moreNavigationController.delegate = self;
use the delegate method of UINavigationController navigationController:willShowViewController:animated: to hide the barButtonItem
Use the following code
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
navigationController.navigationBar.topItem.rightBarButtonItem = Nil;
}
This should work, it worked for me.
There is a more pretty solution:
tabBarController.customizableViewControllers = [];
Prasad Devadiga Thanks man!
In my TabBarViewController.m
#import "TabBarViewController.h"
#interface TabBarViewController ()
#end
#implementation TabBarViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.moreNavigationController.delegate = self;
navigationController:willShowViewController:animated:YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
navigationController.navigationBar.topItem.rightBarButtonItem = Nil;
}
#end
In my TabBarViewController.h
I insert this codes
#import <UIKit/UIKit.h>
#interface TabBarViewController : UITabBarController <UINavigationControllerDelegate, UITabBarControllerDelegate>
#end
This is working on IOS7
After reading all of the above and converting it to Swift 3 I noticed that when one of the view controllers which was shown after selecting the More... tab was a navigationcontroller and had a rightBarButton item, that item was removed as well! Since that is not desired, I came up with the following solution:
//
// CustomizedTabBarController.swift
//
import UIKit
class CustomizedTabBarController: UITabBarController, UINavigationControllerDelegate {
var root : UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
root = self.moreNavigationController.topViewController
// Make sure the icons have the proper tint color
if let view = root?.view {
view.tintColor = UIColor.green
}
self.moreNavigationController.delegate = self
}
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
// Only hide the rightBarButtonItem when this is the morenavigationcontroller
if( viewController == root ){
navigationController.navigationBar.topItem?.rightBarButtonItem = nil
}
}
}
As an extra I also force the icons shown in the moreNavigationController to be green instead of the standard blue tintcolor since I was using green icons on the tabbar as well.
This worked for me
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
tabBarController.customizableViewControllers?.removeAll()
}

UITextView member variable is always NIL in my UIViewController [duplicate]

In my nib, I have a UITextView component.
In my UIViewController I have a UITextView member field and I have used IB to make sure that the two are connected (at least I think I did that by dragging from "Files Owner" and dropping it on the UITextView in IB and selecting my member variable).
Now, when I update the text via setText:, the UITextView still remains blank.
When I break into my code, the member variable (called textView) is nil.
So I am guessing I have not used IB correctly. How can I make sure that this variable is connected to the UITextView graphic element?
Here is the code
// My interface looks like
#import <UIKit/UIKit.h>
#interface DetailsController : UIViewController
{
UITextView *textView;
}
#property (nonatomic, retain) IBOutlet UITextView *textView;
#end;
// and the implementation
#import "DetailsController.h"
#implementation DetailsController
#synthesize textView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
// used pressed the "Done" button
- (IBAction)done
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)setDetails: (NSString *)detail withQuote:(NSString *)quote
{
NSLog(#"%#", textView);
[textView setText:detail];
}
#end
save nib file after connecting outlet, build project and run, it should work if it's connected
I can't see agere you are calling setText, but I think that you try to do it in the initializer. GUI code should not be done before the viewDidLoad in the case of using XIB's or loadView if you make your GUI programatically, or the other viewWill/viewDid... methods.
The reason is because the views are not loaded before they are actually needed, it is called lazy loading. You should Google that for more info.