Why is my storyboard not loaded? - objective-c

I am trying to open a separate storyboard using code for a specific login task.
I created the storyboard and the instance class as follows
+ (instancetype)newStoryboardInstance {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:NSStringFromClass([self class]) bundle:[NSBundle bundleForClass:[self class]]];
AdyenLoginViewController *viewController = [storyboard instantiateInitialViewController];
return viewController;
}
I am then calling this from another class like this
- (void) viewDidAppear:(BOOL)animated {
if([NWTillHelper isDebug] == 1) {
NSLog(#"%s entered", __PRETTY_FUNCTION__);
}
[AdyenLoginViewController newStoryboardInstance];
}
But the loginController is never shown and no error messages are shown, it just finishes loading my normal storyboard and then stops.
The storyboard name etc is correct as per the class name and no compiler errors or warnings are shown
How can I make it load my specific storyboard?

If you are using navigation controller then viewDidAppear will look like this :-
- (void) viewDidAppear:(BOOL)animated {
if([NWTillHelper isDebug] == 1) {
NSLog(#"%s entered", __PRETTY_FUNCTION__);
}
if([AdyenLoginViewController newStoryboardInstance]) {
[self.navigationContoller pushViewController:[AdyenLoginViewController newStoryboardInstance] animated:true];
}
}
Or you can present the viewController like this :-
- (void) viewDidAppear:(BOOL)animated {
if([NWTillHelper isDebug] == 1) {
NSLog(#"%s entered", __PRETTY_FUNCTION__);
}
if([AdyenLoginViewController newStoryboardInstance]) {
[self presentViewController:[AdyenLoginViewController newStoryboardInstance] animated:true completion:^{
}];
}
}

Related

ios7 - how to transition back to self

as per the following storyboard, I have a RootViewController (embedded in a TabBarController), which manage many ViewControllers with a vertical menu.
When this RootViewController did load, I check for client authentication, and if not yet authenticated, I switch to a ConnectViewController for remote authentication
#implementation WDURootViewController
....
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if (!self.client.isAuthorized) {
WDUConnectViewController *cvc = self.connectVC;
[self addChildViewController:cvc];
[self.view addSubview:cvc.view];
[cvc didMoveToParentViewController:self];
}
}
when authentication is successful, I want to come back to the RootViewController...
so in my ConnectViewController, I call back a didConnect() method to notify the RootViewController
#implementation WDUConnectViewController
....
- (IBAction)sendGrantRequest
{
....
[self.client authorizeUser:login password:pwd
onSuccess:^() {
[self.workinOnIt stopAnimating];
[self.connectButton setEnabled:YES];
[(WDURootViewController *)self.parentViewController didConnect];
}
and in the RootViewController, didConnect() method I try to switch the views, in transition from the ConnectViewController to the RootViewController
#implementation WDURootViewController
....
- (void)didConnect
{
[self transitionFrom:self.connectVC To:self];
}
....
- (void)transitionFrom:(UIViewController *)oldC To:(UIViewController *)newC
{
[oldC willMoveToParentViewController:nil];
[self addChildViewController:newC];
[self transitionFromViewController: oldC toViewController: newC
duration: 0.25 options:0
animations: nil
completion:^(BOOL finished) {
[oldC removeFromParentViewController];
[newC didMoveToParentViewController:self];
}];
}
- (WDUConnectViewController *)connectVC {
if (_connectVC == nil) {
UIStoryboard *storyboard = self.storyboard;
_connectVC = [storyboard instantiateViewControllerWithIdentifier:#"WDUConnectViewController"];
_connectVC.client = self.client;
}
return _connectVC;
}
but the transition method is looping... I guess it's not the way to go , what's the simplest way?
I got it , just updating the didConnect() method in the RootViewController
- (void)didConnect
{
[self.connectVC willMoveToParentViewController:nil];
[self.connectVC.view removeFromSuperview];
[self.connectVC removeFromParentViewController];
[self awakeFromNib];
}

popToViewController with interactivePopGestureRecognizer

I'm using the following code
[self.navigationController.interactivePopGestureRecognizer addTarget:self action:#selector(handlePopGesture:)];
- (void)handlePopGesture:(UIGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateBegan)
{
} else if (gesture.state == UIGestureRecognizerStateEnded) {
NSArray *views = [self.navigationController viewControllers];
[self.navigationController popToViewController:[views objectAtIndex:1] animated:YES];
}
but its not working any ideas how to pop view controller using interactivePopGestureRecognizer?
Make sure that your .h file is the delegate.
#interface YOURVIEWCONTROLLER : UIViewController <UIGestureRecognizerDelegate>
In your .m include
self.navigationController.interactivePopGestureRecognizer.delegate = self;
Then your delegate method should recognize your gestures.

UIImagePIckerController appears but the camera does not start

Yes, It might be a duplicate question of this. But since it didn't get an answer, I will be more specific on the case and code:
I have 3 involved UIViewControllers:
WelcomeView - the first one
TakePhotoViewController - the second one who is delegate of the OverlayviewController
OverlayViewController - custom view for the camera.
Scenario:
User enter WelcomeView and clicks on a button to be transfered with segue to TakeView.
UIImageViewController is being opened to take a photo.
The user clicks on cancel button - didCancelCamera method in TakePhotoViewController is being invoked and he returns to WelcomeView
The user leaves the app.
The user re-opens the app and perform step 1 again.
THE IMAGE PICKER IS NOT BEING OPENED. I COULD TAKE A PHOTO AND IT'S OK - BUT THE USER CAN'T SEE WHAT HE IS TAKING.
OverlayViewController.h
#interface OverlayViewController : BaseViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
#property (nonatomic,weak) id<OverlayViewControllerDelegate> delegate;
#property (nonatomic,retain) UIImagePickerController *imagePickerController;
#end
OverlayViewController.m:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate = self;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
}
- (IBAction)takePicture:(id)sender {
[self.imagePickerController takePicture];
}
- (IBAction)cancelImagePicker:(id)sender {
[self.delegate didCancelCamera];
}
- (void) setupImagePicker:(UIImagePickerControllerSourceType) sourceType
{
self.imagePickerController.sourceType = sourceType;
if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
self.imagePickerController.showsCameraControls = NO;
if ([[self.imagePickerController.cameraOverlayView subviews] count] ==0)
{
CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
CGRect newFrame = CGRectMake(0.0, CGRectGetHeight(overlayViewFrame)-self.view.frame.size.height-10.0, CGRectGetWidth(overlayViewFrame), self.view.frame.size.height + 10.0);
self.view.frame = newFrame;
[self.imagePickerController.cameraOverlayView addSubview:self.view];
}
}
}
- (void)finishAndUpdate
{
[NSThread detachNewThreadSelector:#selector(threadStartAnimating:) toTarget:self withObject:nil];
[self.delegate didFinishWithCamera]; // tell our delegate we are done with the camera
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self finishAndUpdate];
}
TakePhotoViewController.h
#interface TakePhotoViewController : BaseViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,OverlayViewControllerDelegate>
#property (nonatomic, retain) OverlayViewController *overlayViewController;
#end
TakePhotoViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
// Insert the overlay
self.overlayViewController = (OverlayViewController *)[sb instantiateViewControllerWithIdentifier:#"Overlay"];
self.overlayViewController.delegate = self;
}
- (void)viewDidUnload
{
self.overlayViewController = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)openImagePicker {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
[self showImagePicker:UIImagePickerControllerSourceTypeCamera];
}
else{
[self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}
}
- (void)viewDidAppear:(BOOL)animated{
if (appDelegate.shouldOpenPicker){
[self openImagePicker];
}
}
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self.overlayViewController setupImagePicker:sourceType];
[self presentViewController:self.overlayViewController.imagePickerController animated:YES completion:nil];
}
}
-(void)didCancelCamera{
[[self.overlayViewController.imagePickerController presentingViewController] dismissViewControllerAnimated:NO completion:^ {
[self performSegueWithIdentifier:#"fromTakeToWelcome" sender:self];
}];
}
I found the bug.
The method
-(void)didCancelCamera from TakePhotoViewController is being called when the user clicks on - (IBAction)cancelImagePicker:(id)sender in OverlayViewController.
However, somehow the code in didCancelCamera causes viewDidAppear method of TakePhotoViewController to be invoked again and reopen the image picker.
I have no idea why
[[self.overlayViewController.imagePickerController presentingViewController] dismissViewControllerAnimated:NO completion:^ {
[self performSegueWithIdentifier:#"fromTakeToWelcome" sender:self];
}];
causes the viewDidAppear method of that view (TakePhoto) being recalled again.
Hope that it will help someone

Check to see if UIViewController has already been created

I'm trying to check if a viewcontroller has already been created. If the view already exists then it should become the current view. If it doesn't exist then it should be created and made visible.
My code is
DemoViewController *demoController = [DemoViewController alloc];
for(DemoViewController *view in self.navigationController.viewControllers)
{
if([view isKindOfClass:[DemoViewController class]])
{
viewExists=true;
demoController=view;
}
}
if (!viewExists) {
demoController initWithNibName:#"DemoViewController" bundle:nil;
}
[view release];
[demoController release];
I'm not sure where I'm going wrong but it would appear that the for loop isn't being executed.
Any help would be great!
Try like this:
DemoViewController* demoController = nil;
for(int vv=0; vv<[self.navigationController.viewControllers count]; ++vv) {
NSObject *vc = [self.navigationController.viewControllers objectAtIndex:vv];
if([vc isKindOfClass:[DemoViewController class]]) {
demoController = (DemoViewController*)vc;
}
}
if (demoController == nil) {
demoController = [[DemoViewController alloc] initWithNibName:#"DemoViewController" bundle:nil];
// Do we need to push it into navigation controller?
}
[demoController release];

Navigating to another view iPhone sdk?

I want to navigate to another view. I have like a now playing button for my radio application. I would like the view to mentain its state... the view that I want to navigate to has a text field for url of the radion and a label for radio name. so how can I mentain the state of that view because right now when ever I try to navigate it navigates fine but the text field changes.. so what can I do to not change the text?
here is some code sorry about wierd names
if (self.bookDetailViewController_1 == nil)
{
iPhoneStreamingPlayerViewController *aBookDetail = [[iPhoneStreamingPlayerViewController alloc] initWithNibName:#"iPhoneStreamingPlayerView" bundle:nil];
self.bookDetailViewController_1= aBookDetail;
[aBookDetail release];
}
Music_appAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
[delegate.booksNavController pushViewController:bookDetailViewController_1 animated:YES];
Singleton implementation
//.h
+ (className *)sharedInstance;
//.m
static className* sharedInstance = nil;
#implementation className
+ (className*)sharedInstance {
if (!sharedInstance) {
sharedInstance = [[super allocWithZone:NULL] init];
}
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
return [[self sharedInstance] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (NSUInteger)retainCount {
return NSUIntegerMax;
}
- (void)release { }
- (id)autorelease {
return self;
}
#end