I have a very simple app with very little code. In my ViewController I have done no code, I have only added a navigation bar which contains a next button with a modal to the VideoController. What I would like to achieve is after the next button is pushed in ViewController, allow the user to view VideoController for 5 seconds and then automatically return to the previous ViewController. Here is the code I have added in my VideoController.m
#import "VideoController.h"
#interface VideoController ()
#end
#implementation VideoController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//code added
[self performSelector:#selector(goBack) withObject:nil afterDelay:5.0];
}
//code added
-(void)goBack{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
\
#end
I am not receiving any errors however this is not reverting me back to the previous page as desired. What am I missing?
try this code
- (void)viewDidLoad
{
[super viewDidLoad];
//code added
//[self performSelector:#selector(goBack) withObject:nil afterDelay:5.0];
[NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:#selector(goBack)
userInfo:nil repeats:NO];
}
//code added
-(void)goBack{
[self.navigationController popViewControllerAnimated:YES];
}
Related
I am trying to build a splash screen so I want the 1st View Controller to move to 2nd ViwController automatically after 3.0 sec I have tried the below method but an infinite loop has started what should i do ,how should I stop on second view controller.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"%p", self);
NSLog(#"1st Controller");
[self.navigationController setNavigationBarHidden:YES animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self loadingNextView];
});
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)loadingNextView{
LoginViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
//LoginViewController.h
#interface LoginViewController : ViewController
#end
//LoginViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"2nd View Controller");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
see after three seconds how the loop is working .
Why don't you use NSTimer class. Simply create a timer for 10 seconds when 10 seconds passed timer will trigger the event and in that event you can move to another controller. Create a timer like this
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:NO];
- (void)targetMethod:(NSTimer*)timer {
[self loadingNextView];
}
It is easy to do, you can add a property to judge if is first come in the vc1:
The result:
In fitst VC:
#import "ViewController.h"
#import "LoginViewController.h"
#interface ViewController ()
#property(nonatomic, assign) BOOL isFirstTime;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_isFirstTime = YES;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
/* [self performSelector:#selector(loadingNextView)
withObject:nil afterDelay:1.0f]; */
if (_isFirstTime == NO) {
return;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self loadingNextView];
_isFirstTime = NO;
});
}
- (void)loadingNextView{
LoginViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
Try this
[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:false block:^(NSTimer * _Nonnull timer) {
[self loadingNextView];
}];
So a little advise, you should think about presenting a loginViewController instead pushing it to the NavigationController stack. If you push it, you have to remove the back buttons if you don't want the user to come back to the other ViewController. If you present it you can be sure that the user can't go back to the firstVc without entering his login data.
In the second Vc you then can dismiss the Vc or you can present a new ViewController.
[self presentViewController:vc animated:YES completion:nil];
You can achieve you goal by using this.
#import "DrawingViewController.h"
#import "SecondViewController.h"
#interface DrawingViewController ()
#end
#implementation DrawingViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[button removeFromSuperview];
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(target:) userInfo:nil repeats:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)target:(NSTimer*)timer {
SecondViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
Finally got the answer.
#import "ViewController.h"
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"%p", self);
[self.navigationController setNavigationBarHidden:YES animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSLog(#"1st controller");
[self loadingNextView];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadingNextView{
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
//LoginViewController.h
#import "ViewController.h"
#interface LoginViewController : UIViewController
#end
//LoginViewController.m
#import "LoginViewController.h"
#interface LoginViewController ()
#end
#implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"2nd View Controller");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
I have a single storyboard where a majority of the views should be in portrait mode but one should be in Landscape. I've down a significant amount of searching but keep coming up a little blank.
My current implementation which isn't exactly seeming to work as I hoped consists of the following bits of code I've pieced together from this website:
I've made a RotationalViewController which extends from UINavigationController
#import "RotationControlledViewController.h"
#interface RotationControlledViewController ()
#end
#implementation RotationControlledViewController
- (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.
}
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
UIInterfaceOrientation ret = [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
return ret;
}
#end
And then I've made a PortraitViewController
#import "PortraitViewController.h"
#interface PortraitViewController ()
#end
#implementation PortraitViewController
- (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.
}
-(BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
and a LandscapeViewController
#import "LandscapeViewController.h"
#interface LandscapeViewController ()
#end
#implementation LandscapeViewController
- (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.
}
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
#end
I have played around with changing some of the settings but to no avail. My general plan of attack has been to make specific view controllers inherit from either LandscapeViewController or PortraitViewController if I want to lock them to a specific orientation.
Things seem to be working for portrait view ( i can lock controllers into portrait view) but I seem unable to load my 1 landscape view and have it a) auto rotate to landscape upon load, or b) rotate to landscape at all.
Any suggestions would be very appreciated.
Applying the solution presented in:
Force controllers to Change their orientation in Either Portrait or Landscape
I was able to make things work.
In both my PortraiteViewController and LandscapeViewControllers I modified the viewDidLoad method to:
PortraiteViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
LandscapeViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
This solution MAY stop working after IOS7 because both presentModalViewController and dismissModalViewController are deprecated.
I have a master utility application embedded in a tab bar. Now, for this tab bar, i have the following.
#interface MainTabViewController : UITabBarController< UITabBarControllerDelegate>{
}
#end
#implementation MainTabViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(#"One");
if(self.tabBarController.selectedIndex==1)
{
NSLog(#"clicked");
}
if (self.tabBarController.selectedIndex==2) {
NSLog(#"Helo");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
The code is not reaching the didSelectViewController at all. I am new to IOS. Could anyone plz guide where i am going wrong??
Add delegate method to the UITabBarController
either using storyboard (if you are using)
or as it is UITabBarController do using
self.delegate = self ;
I have an app I am working on, and I am trying to have the logo on for a bit longer, and fade out/slide out/effect when it's done.
Here's my setup:
The Tab Bar controller is not letting me place an Image View inside it, so I created a view to have it on.
I am trying to have the logo stay on for a bit, fade out, then automatically switch the view (Segue) to the Tab Bar controller.
This is what I get out of it: http://youtu.be/l4jL0BfpR2k
So here's my code:
//
// BDNLogoViewController.m
// Bronydom Network
//
// Created by name on 10/1/13.
// Copyright (c) 2013 name. All rights reserved.
//
#import "BDNLogoViewController.h"
#import "BDNTabBarController.h"
#import "BDNFirstViewController.h"
#interface BDNLogoViewController ()
#end
#implementation BDNLogoViewController
- (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.
[UIView animateWithDuration:1 animations:^{
_imageview.alpha = 0;
}];
//BDNTabBarController *viewController = [[BDNTabBarController alloc] init];
//[self.navigationController pushViewController:viewController animated:YES];
(void)#selector(seguePerform:);
}
- (void)seguePerform:(id)sender
{
//BDNTabBarController *myNewVC = [[BDNTabBarController alloc] init];
// do any setup you need for myNewVC
[self performSegueWithIdentifier:#"open" sender:sender];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Yes, "open" is defined as the segue id.
Do you guys have any ideas on how I could fix this?
To fix, add this
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
[self performSegueWithIdentifier:#"open" sender:self];
}
Remove this from your code
(void)#selector(seguePerform:);
// and all other unnecessary segue stuff you had
pass data from FirstViewController to DetailViewController. i can not set the text of label in DetailViewController; FirstViewController is a tableview and it is good.
i use method updateRowNumber to set the rowNumber . and in DetailViewController, i can use debugger to see the rowNumber is correct. but the label's text is not showed on the view.
anyone can help me out?
in my FirstViewController
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (dvController == nil)
{
DetailViewController *aController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
self.dvController = aController;
[aController release];
}
[[self navigationController] pushViewController:dvController animated:YES];
[dvController updateRowNumber:indexPath.row];
}
in my DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController
{
int rowNumber;
IBOutlet UILabel *message;
}
#property(readwrite) int rowNumber;
#property(nonatomic, retain) IBOutlet UILabel *message;
- (void) updateRowNumber:(int) theindex;
#end
in my DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
#synthesize message, rowNumber;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void) updateRowNumber: (int) theindex
{
rowNumber = theindex + 1;
message.text = [NSString stringWithFormat:#"row %i was clicked", rowNumber];
}
- (void)dealloc
{
[message release];
[super dealloc];
}
- (void)viewDidLoad
{
message.text = [NSString stringWithFormat:#"row %i was clicked ", rowNumber];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated
{
//message.text = [NSString stringWithFormat:#"row %i was clicked ", rowNumber];
[super viewWillAppear: animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear: animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You'll need to learn how the view is loaded, the process is well described in the documentation
What happens is that the view and all the outlets are nil until the view is loaded, you can make sure it is loaded by calling self.view; before configuring the outlet at updateRowNumber:
Please also note, you are better to call [super viewDidLoad] in the beginning of the overridden viewDidLoad, it makes sense as you need to let UIViewContorller to do it's staff before you do some customized logic, the dealloc is different as you need to do your logic before the standard NSObject -dealloc fires. Hope it's understandable.