How to navigate from UIView to UIViewController - objective-c

i am aware that uiview does not have navigational control. Thus, i am not able to push from the uiview to the next uiviewcontroller.
here is the code of my method. Please help. Thanks!
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
CGPoint pointInView = [recognizer locationInView:self];
NSLog(#"%f , %f",pointInView.x,pointInView.y);
if ((pointInView.x >= (231.0) && pointInView.x <=(293) && pointInView.y >= (193) && pointInView.y <=(218)) )
{
// how to push to next viewcontroller here?
UIView *view = [UIGestureRecognizer view]; //what should i do for this code?
[self viewController];
}
}
-(TestViewViewController*)viewController
{
for (UIView *next = [self superview]; next; next = next.superview)
{
UIResponder *nextResponder = [next nextResponder];
if([nextResponder isKindOfClass:[TestViewViewController class]])
{
return (TestViewViewController*)nextResponder;
}
}
return nil;
}

If you want to get viewcontroller having a view than you can use nextResponder method
So first you get the view from gestureRecognizer:
UIView *view = [gestureRecognizer view];
and than search for the viewcontroller containing the view
Look here
Edit:
if ((pointInView.x >= (231.0) && pointInView.x <=(293) && pointInView.y >= (193) && pointInView.y <=(218)) )
{
// how to push to next viewcontroller here?
UIView *view = [recognizer view]; //what should i do for this code?
UIViewController *viewController = [self getViewControllerFromView:view];
if(viewController) {
// do whatever you want...
UIViewController *newViewController = ...
[viewController.navigationController pushViewController:newViewController animated:YES];
}
}
...
I rewrote method get ViewController to your case. To get it from some view:
-(UIViewController*)getViewControllerFromView:(UIView*) view
{
if([[view nextResponder] isKindOfClass:[UIViewController class]])
{
return (UIViewController*)[view nextResponder];
}
else {
for (UIView *next = [view superview]; next; next = next.superview)
{
UIResponder *nextResponder = [next nextResponder];
if([nextResponder isKindOfClass:[UIViewController class]])
{
return (UIViewController*)nextResponder;
}
}
}
return nil;
}
new edit::
your EAGLView has #property (nonatomic, retain) UIViewController *viewController;
ADD #synthesize viewController = _viewController; to .m file.
CoreMotionAppDelegate::
MainViewController * itemsViewController = [[MainViewController alloc]init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:itemsViewController];
glView.viewController = itemsViewController;
[glView startAnimation];
[[self window]setRootViewController:navController];
And than in your scrollViewDoubleTapped method just
EAGLView *v = (EAGLView*)[recognizer view];
UIViewController *viewController = v.viewController;

Related

How do I add multiple ViewControllers to my UIPageViewController?

so I'm pretty new to Obj-C and have tried looking at sample code and other online resources to answer my question, but I can't seem to find anything that really helps. Essentially what I'm trying to do is to add multiple UIViewControllers that I created in Storyboard to a UIPageViewController - the first place I looked to for help was Xcode itself by using a template of a PageBased application. That actually helped quite a lot and I got a PageController up and running. Then I turned to online resources but most (if not all of them) use a single viewController (like this). Then I turned to StackOverflow and found the following - How to implement UIPageViewController that utilizes multiple ViewControllers.
The above resources got me this far:
In PageViewController.h:
#import <UIKit/UIKit.h>
#interface PageViewController : UIPageViewController <UIPageViewControllerDataSource>
#end
In PageViewController.m:
#import "PageViewController.h"
#interface PageViewController ()
#end
#implementation PageViewController {
NSArray *viewControllers;
UIViewController *first;
UIViewController *second;
UIViewController *third;
UIViewController *fourth;
UIViewController *fifth;
}
- (UIViewController *)first {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Secondary" bundle:nil];
first = [sb instantiateViewControllerWithIdentifier:#"1"];
return first;
}
- (UIViewController *)second {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Secondary" bundle:nil];
second = [sb instantiateViewControllerWithIdentifier:#"2"];
return second;
}
- (UIViewController *)third {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Secondary" bundle:nil];
third = [sb instantiateViewControllerWithIdentifier:#"3"];
return third;
}
- (UIViewController *)fourth {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Secondary" bundle:nil];
fourth = [sb instantiateViewControllerWithIdentifier:#"4"];
return fourth;
}
- (UIViewController *)fifth {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Secondary" bundle:nil];
fifth = [sb instantiateViewControllerWithIdentifier:#"5"];
return fifth;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.dataSource = self;
// Aggancio il view controller iniziale.
[self setViewControllers:#[self.first]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
UIViewController *nextViewController = nil;
if (viewController == self.first) {
nextViewController = self.second;
}
if (viewController == self.second) {
nextViewController = self.third;
}
if (viewController == self.third) {
nextViewController = self.fourth;
}
if (viewController == fourth) {
nextViewController = self.fifth;
}
return nextViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
UIViewController *prevViewController = nil;
if (viewController == self.fifth) {
prevViewController = self.fourth;
}
if (viewController == self.fourth) {
prevViewController = self.third;
}
if (viewController == self.third) {
prevViewController = self.second;
}
if (viewController == self.second) {
prevViewController = self.first;
}
return prevViewController;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
All this code does is load the first view controller which is now swipe-able, but doesn't actually swipe towards anything - why is this?. What am I doing wrong? The aforementioned resources used titles and pages to create views and I really don't know how to go about this. Would anyone mind guiding me or nudging me in the correct direction? Any help would be greatly appreciated. Thanks!
Here is the fully working code if anyone is interested. Took me a while to get it right, but eventually this worked perfectly!
#import "PageViewController.h"
#interface PageViewController ()
#property (nonatomic, retain) UIViewController *first;
#property (nonatomic, retain) UIViewController *second;
#property (nonatomic, retain) UIViewController *third;
#property (nonatomic, retain) UIViewController *fourth;
#property (nonatomic, retain) UIViewController *fifth;
#end
#implementation PageViewController {
NSArray *viewControllers;
}
- (UIViewController *)first {
if (!_first) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Secondary" bundle:nil];
_first = [sb instantiateViewControllerWithIdentifier:#"1"];
}
return _first;
}
- (UIViewController *)second {
if (!_second) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Secondary" bundle:nil];
_second = [sb instantiateViewControllerWithIdentifier:#"2"];
}
return _second;
}
- (UIViewController *)third {
if (!_third) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Secondary" bundle:nil];
_third = [sb instantiateViewControllerWithIdentifier:#"3"];
}
return _third;
}
- (UIViewController *)fourth {
if (!_fourth) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Secondary" bundle:nil];
_fourth = [sb instantiateViewControllerWithIdentifier:#"4"];
}
return _fourth;
}
- (UIViewController *)fifth {
if (!_fifth) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Secondary" bundle:nil];
_fifth = [sb instantiateViewControllerWithIdentifier:#"5"];
}
return _fifth;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.dataSource = self;
// Aggancio il view controller iniziale.
[self setViewControllers:#[self.first]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
UIViewController *nextViewController = nil;
if (viewController == self.first) {
nextViewController = self.second;
}
if (viewController == self.second) {
nextViewController = self.third;
}
if (viewController == self.third) {
nextViewController = self.fourth;
}
if (viewController == self.fourth) {
nextViewController = self.fifth;
}
return nextViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
UIViewController *prevViewController = nil;
if (viewController == self.fifth) {
prevViewController = self.fourth;
}
if (viewController == self.fourth) {
prevViewController = self.third;
}
if (viewController == self.third) {
prevViewController = self.second;
}
if (viewController == self.second) {
prevViewController = self.first;
}
return prevViewController;
}
#end
This is old, but it helped me out a lot. For some reason your code had some problems for me, and I wasn't using storyboard. For anyone's future reference, this is the code I'm using that works:
#import "PageController.h"
#import "SeasonTixViewController.h"
#import "SeasonTicketPricesViewController.h"
#import "ArenaLayoutSeasonTixViewController.h"
#interface PageController ()
#property (nonatomic, retain) UIViewController *first;
#property (nonatomic, retain) UIViewController *second;
#property (nonatomic, retain) UIViewController *third;
#end
#implementation PageController {
NSArray *viewControllers;
}
- (UIViewController *)first {
if (!_first) {
SeasonTixViewController *first = [[SeasonTixViewController alloc] init];
_first = first;
}
return _first;
}
- (UIViewController *)second {
if (!_second) {
SeasonTicketPricesViewController *second = [[SeasonTicketPricesViewController alloc] init];
_second = second;
}
return _second;
}
- (UIViewController *)third {
if (!_third) {
ArenaLayoutSeasonTixViewController *third = [[ArenaLayoutSeasonTixViewController alloc] init];
_third = third;
}
return _third;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
[[self.pageController view] setFrame:self.view.bounds];
self.pageController.dataSource = self;
[self.pageController setViewControllers:#[self.first]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
UIViewController *nextViewController = nil;
if (viewController == self.first) {
nextViewController = self.second;
}
if (viewController == self.second) {
nextViewController = self.third;
}
return nextViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
UIViewController *prevViewController = nil;
if (viewController == self.third) {
prevViewController = self.second;
}
if (viewController == self.second) {
prevViewController = self.first;
}
return prevViewController;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
// The number of items reflected in the page indicator.
return 3;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
// The selected item reflected in the page indicator.
return 0;
}
#end

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.

How can i move to myScene from UIViewController?

I have 2 viewcontroller. In the first viewcontroller, there is the introduction and logo of the game and in the second viewcontroller it loads the skscene. And I travel from my first viewcontroller to second viewcontroller with a button click. But it becones error. My code is in the second viewcontroller is:
#interface MPAPViewSceneController()
#property (nonatomic, retain) MPAPMyScene *targetScene;
#property SKView *spriteView;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
_spriteView = (SKView *)self.view;
_spriteView.showsFPS = YES;
_spriteView.showsNodeCount = YES;
_spriteView.showsDrawCount = YES;
}
-(void)viewWillAppear:(BOOL)animated {
self.targetScene = [[MPAPMyScene alloc] initWithSize:CGSizeMake(768.0f, 1024.0f)];
[_spriteView presentScene:self.targetScene];
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:YES];
self.targetScene = nil;
}
And in the scene I have written the code given below:
#interface MPAPMyScene()
#property (nonatomic) BOOL contentCreated;
#end
#implementation MPAPMyScene
-(void)didMoveToView:(SKView *)view {
if (self.contentCreated == NO) {
[self createSceneContents];
self.contentCreated = YES;
}
}
-(void)willMoveFromView:(SKView *)view {
[self removeAllChildren];
}
-(void)createSceneContents {
self.backgroundColor = [UIColor blueColor];
self.scaleMode = SKSceneScaleModeAspectFit;
}
Try changing this
_spriteView = (SKView *)self.view;
to
_spriteView = (SKView *)self.targetScene;

Working with a container view

This is my situation. I have a viewController. With at the top three buttons and below the buttons a containerview. When you push a button, the container view should change.
This is what I have done in storyboard.
Dragged a view controller into my storyboard. added 3 buttons and a containerview into it.
This view controller is off class viewController
Dragged a second viewcontroller into it. And controlled dragged from the containerView to this view controller. and selected embed segue.
Now in code I do the following for controller viewController
-(IBAction)chooseFirstController:(id)sender {
[self.childViewControllers.lastObject switchToFirst];
}
-(IBAction)chooseSecondController:(id)sender {
[self.childViewControllers.lastObject switchToSecond];
}
-(IBAction)chooseThirdController:(id)sender {
[self.childViewControllers.lastObject switchToThird];
}
And for my containerController.h I do the following.
#interface ContainerViewController : ViewController
#property (nonatomic,strong) FirstController *cont1;
#property (nonatomic,strong) SecondController *cont2;
#property (nonatomic,strong) ThirdController *cont3;
#property (nonatomic,strong) ContainerViewController *currentController;
And in my container.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.cont1 = [[FirstController alloc]initWithNibName:#"FirstController" bundle:nil];
self.cont2 = [[SecondController alloc]initWithNibName:#"SecondController" bundle:nil];
self.cont3 = [[ThirdController alloc]initWithNibName:#"ThirdViewController" bundle:nil];
[self addChildViewController:self.cont1];
self.currentController = self.cont1;
[self.view addSubview:self.cont1.view];
}
-(void)switchToFirst {
if (self.currentController != self.cont1) {
[self addChildViewController:self.cont1];
[self moveToNewController:self.cont1];
}
}
-(void)switchToSecond {
if (self.currentController != self.cont2) {
[self addChildViewController:self.cont2];
[self moveToNewController:self.cont2];
}
}
-(void)switchToThird {
if (self.currentController != self.cont3) {
[self addChildViewController:self.cont3];
[self moveToNewController:self.cont3];
}
}
-(void)moveToNewController:(id) newController {
[self.currentController willMoveToParentViewController:nil];
[self transitionFromViewController:self.currentController toViewController:newController duration:.6 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{}
completion:^(BOOL finished) {
[self.currentController removeFromParentViewController];
[newController didMoveToParentViewController:self];
self.currentController = newController;
}];
}
But I keep getting error "No known instance method for selector switchtoFirst" in my IBActions.
Can anybody help me?
Thanks in advance.
I would guess that lastObject is returning a UIViewController and doesn't know it's a ContainerViewController - hence doesn't know about the switchtoFirst method.
Try something like:
-(IBAction)chooseFirstController:(id)sender {
UIViewController *vc = self.childViewControllers.lastObject;
if([vc isKindOfClass:[ContainerViewController class]]) {
ContainerViewController *containerVC = (ContainerViewController *)vc;
[containerVC switchtoFirst];
}
}

Trying to understand how to use xib files

I have a custom view that I'm using in a xib file. I load the view and add it to a window. It adds the view just fine as I can see the default text of the labels in the view, but when I try to change the label with a method call, it doesn't change the text.
The custom view isn't anything to fancy, just draws a rounded, transparent background.
NotificationView.h
#import <Cocoa/Cocoa.h>
#interface NotificationView : NSView
#property (weak) IBOutlet NSTextField *primaryLabel;
#property (weak) IBOutlet NSTextField *secondaryLabel;
#property (weak) IBOutlet NSTextField *identifierLabel;
#end
NotificationView.m
#implementation NotificationView
#synthesize primaryLabel;
#synthesize secondaryLabel;
#synthesize identifierLabel;
- (id) initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self)
{
return self;
}
return nil;
}
- (void)drawRect:(NSRect)dirtyRect
{
NSColor *bgColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.6];
NSRect rect = NSMakeRect([self bounds].origin.x + 3, [self bounds].origin.y + 3, [self bounds].size.width - 6, [self bounds].size.height - 6);
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:5.0 yRadius:5.0];
[path addClip];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[NSColor redColor]];
[shadow setShadowBlurRadius:2.0f];
[shadow setShadowOffset:NSMakeSize(0.f, -1.f)];
[shadow set];
[bgColor set];
NSRectFill(rect);
[super drawRect:dirtyRect];
}
#end
In the xib I have a custom view set to the type NotificationView. I've added 3 labels to the view and connected them to the above IBOutlets. (I ctrl-click & drag from the label to the .h file to make the connection.)
I'm loading the view and adding it to a window with the following method. It looks through an array of windows, if an existing match is found it used that window, if not it creates a new window.
- (void) popupNotificationWithTag:(NSString *)tag fade:(double)msFade lineOne:(NSString *)lineOneText lineTwo:(NSString *)lineTwoText
{
NotificationWindow *notificationWindow;
NotificationWindow *tmpWindow;
NSEnumerator *enumerator;
// Walk the notification windows in the array
enumerator = [self.notificationWindows objectEnumerator];
if(enumerator)
{
while((tmpWindow = [enumerator nextObject]))
{
if([tmpWindow.tag isEqualToString:tag])
{
notificationWindow = tmpWindow;
}
}
}
// Make a new notification window
if (!notificationWindow)
{
int width = [[NSScreen mainScreen] frame].size.width;
int height = [[NSScreen mainScreen] frame].size.height;
notificationWindow = [[NotificationWindow alloc] initWithRect:NSMakeRect(width - 420, height - 130, 400, 100)];
NSNib *nib = [[NSNib alloc] initWithNibNamed:#"Notification" bundle: nil];
NSArray *objects;
[nib instantiateNibWithOwner:self topLevelObjects:&objects];
for (id obj in objects) {
if ([[obj class] isSubclassOfClass:[NSView class]])
[notificationWindow setContentView:obj];
}
[notificationWindow setTag:tag];
[self.notificationWindows addObject:notificationWindow];
}
// Display window
[notificationWindow makeKeyAndOrderFront:nil];
[notificationWindow display];
notificationWindow.fadeOut = msFade;
[notificationWindow setPrimaryText:lineOneText];
[notificationWindow setSecondaryText:lineTwoText];
[notificationWindow setIdentifierText:tag];
}
The window class is NotificationWindow.h
#import <Foundation/Foundation.h>
#interface NotificationWindow : NSWindow
#property (nonatomic, strong) NSString *tag;
#property (nonatomic) double fadeOut;
- (id)initWithRect:(NSRect)contentRect;
- (void) setPrimaryText:(NSString *)text;
- (void) setSecondaryText:(NSString *)text;
- (void) setIdentifierText:(NSString *)text;
#end
NotificationWindow.m
#import "NotificationWindow.h"
#import "NotificationView.h"
//===========================================================================================================================
// Private call properties and methods
//===========================================================================================================================
#interface NotificationWindow()
#property (nonatomic,strong) NSTimer *timerFade;
- (void) timerFadeFired;
#end
//===========================================================================================================================
//===========================================================================================================================
#implementation NotificationWindow
//===========================================================================================================================
// Property Getters and Setters
//===========================================================================================================================
#synthesize tag = _tag;
#synthesize fadeOut = _fadeOut;
#synthesize timerFade = _timerFade;
//===========================================================================================================================
// Public methods
//===========================================================================================================================
- (id)initWithRect:(NSRect)contentRect
{
if (self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO]) {
[self setLevel: NSScreenSaverWindowLevel];
[self setBackgroundColor: [NSColor clearColor]];
[self setAlphaValue: 1.0];
[self setOpaque: NO];
[self setHasShadow: NO];
[self setIgnoresMouseEvents: YES];
[self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
[self orderFront: NSApp];
self.fadeOut = -1;
// Start our timer to deal with fadeing the window
self.timerFade = [NSTimer scheduledTimerWithTimeInterval:0.001
target:self
selector:#selector(timerFadeFired)
userInfo:nil
repeats:YES];
return self;
}
return nil;
}
- (BOOL) canBecomeKeyWindow
{
return YES;
}
- (void) display
{
[super display];
[self setAlphaValue:1.0];
}
- (void) setPrimaryText:(NSString *)text
{
NotificationView *view = self.contentView;
view.primaryLabel.stringValue = text;
}
- (void) setSecondaryText:(NSString *)text
{
NotificationView *view = self.contentView;
view.secondaryLabel.stringValue = text;
}
- (void) setIdentifierText:(NSString *)text
{
NotificationView *view = self.contentView;
view.identifierLabel.stringValue = text;
}
//===========================================================================================================================
// Private methods
//===========================================================================================================================
- (void) timerFadeFired
{
[self orderFront:NSApp];
if (self.fadeOut > 0)
{
self.fadeOut--;
}
else if (self.fadeOut == 0)
{
if (self.alphaValue > 0)
self.alphaValue -= 0.002;
else
self.fadeOut = -1;
}
}
#end
So I assume I'm doing something wrong connecting the labels to the IBOutlets, but I can't figure out what. I suppose I could create the view in code, but I was trying to be good and use the interface builder.
I'm in XCode 4.2.1.