Xcode: How do I change UIPageControl value with swipe gesture? - objective-c

I have a quick question I was hoping you guys could help me answer. Right now I have a UIPageControl in a storyboard which changes an image depending on what dot you are on, however, as of now you have to press on the dot to change through the dots/images, how can I change through the images/dots by swiping?
Here is my code for my .h
#import <UIKit/UIKit.h>
#interface PageViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *dssview;
- (IBAction)changephoto:(UIPageControl *)sender;
#end
Here is my code for my .m
#import "PageViewController.h"
#interface PageViewController ()
#end
#implementation PageViewController
- (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.
}
- (IBAction)changephoto:(UIPageControl *)sender {
_dssview.image = [UIImage imageNamed:
[NSString stringWithFormat:#"%d.jpg",sender.currentPage+1]];
}
#end
Any help would be greatly appreciated.
Thanks

You can add UISwipeGestureRecognizer to your view and in the selector method of UISwipeGestureRecognizer based on the direction update the UIPageControl object, either increment the current page or decrement.
You can refer the below code.
Adding swipe gesture to the view controller
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
Swipe gesture selector
- (void)swipe:(UISwipeGestureRecognizer *)swipeRecogniser
{
if ([swipeRecogniser direction] == UISwipeGestureRecognizerDirectionLeft)
{
self.pageControl.currentPage -=1;
}
else if ([swipeRecogniser direction] == UISwipeGestureRecognizerDirectionRight)
{
self.pageControl.currentPage +=1;
}
_dssview.image = [UIImage imageNamed:
[NSString stringWithFormat:#"%d.jpg",self.pageControl.currentPage]];
}
Add an outlet to the UIPageControl in the .h file
#interface PageViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *dssview;
#property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
- (IBAction)changephoto:(UIPageControl *)sender;
#end

Related

add custom UIView and xib into storyboard

I have a custom UIView and xib called DialPad and I'm trying to use this object in my storyboard, but the custom DialPad isn't being add as a subview with the buttons
my custom DialPad :UIView
#implementation DialPad
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor clearColor];
}
return self;
}
my custom xib called DialPad
and my view controllers look like this
#import <UIKit/UIKit.h>
#import "DialPad.h"
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIView *dial;
#end
#import "ViewController.h"
#interface ViewController ()
{
DialPad *d;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
d = [[DialPad alloc] initWithFrame:self.dial.frame];
[self.dial addSubview:d];
}
#end
and my storyboard
but the border around the view looks strange
#import "DialPad.h"
#import <QuartzCore/QuartzCore.h>
#implementation DialPad
- (void)drawRect:(CGRect)rect
{
// Drawing code
self.layer.borderColor = [UIColor redColor].CGColor;
self.layer.borderWidth = 1;
}
#end
For loading a view from xib file, you should do this:
d = [[[NSBundle mainBundle] loadNibNamed:#"DialPad" owner:nil options:nil]
firstObject];
[self.dial addSubview:d];
Border: try to add border code in - (void)awakeFromNib

iOS / Objective-C: UIView delegates not called

i´ve got a uiview in a scrollview which is in a viewcontroller and it seems that the delegates are not called because i added them to the uiview.
Is it possible to solve this problem? (I want to keep the delegates in the uiview because it seems to be cleaner code)
Maik
EDIT:
in .h file:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface RHCHuntingReportViewPageTwo : UIView <MKMapViewDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
in .m file:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *pin=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
pin.image = [UIImage imageNamed:#"map_pin.png"];
pin.draggable = NO;
pin.canShowCallout = YES;
return pin;
}
I added the delegate and outlet correctly in storyboard.
edit 2:
in .m file:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
RHCAnnotation *annotation = [[RHCAnnotation alloc] init];
annotation.title = #"title";
annotation.subtitle = #"subtitle";
annotation.image = [UIImage imageNamed:#"map_tipp.png"];
annotation.coordinate = CLLocationCoordinate2DMake(57.242465, 2.533579);
mapView.region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 250, 250);
[mapView addAnnotation:annotation];
}
return self;
}

How to send array to subview and then print into label?

Working over storyboard.
I've created View Controller (storyboard) and then in middle of content added subview from xib file.
I want to add xib (UIView) like as subview into ViewController and send object with data and print that data into label but I don't know how.
Here is my code.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
UIView *subView;
}
#property (strong, nonatomic) IBOutlet UIView *subView; //connected over IB
#end
ViewController.m
#import "OfferViewController.h"
#import "OfferLocation.h"
#interface OfferViewController ()
#end
#implementation OfferViewController
#synthesize subView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
OfferLocation *location = [[OfferLocation alloc] initWithFrame:CGRectZero];
[self.subView addSubview:location];
}
...
#end
and here is subview:
OfferLocation.h
#import <UIKit/UIKit.h>
#interface OfferLocation : UIView{
UIView *view;
UILabel *locationLabel;// here is that label taht I want to print into
}
#property (nonatomic, retain) IBOutlet UIView *view;// connected over IB
#property (nonatomic, retain) IBOutlet UILabel *locationLabel;
#end
OfferLocation.m
#import "OfferLocation.h"
#implementation OfferLocation
#synthesize view, locationLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
locationLabel.text = #"some text"; //this is not working
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:#"OfferLocation" owner:self options:nil];
UIView *tempView = [subviewArray objectAtIndex:0];
[self addSubview:tempView];
}
return self;
}
subView never seems to be initialized and loaded to the view or presented. Therefore you cannot see it.
Try calling:
self.view = self.subView // or subView; // or [self.view addSubview:self.subView // or subview];
or something similiar
Then declare a UILabel and initialize it with whatever text you want and add it to subview:
UILabel *label = [[UILabel alloc] initWithString:#"StackOverflow rocks!"];
[self.subView addSubview:label];
// or [self.view addSubview:label];
Differing on how you set subView.
What is this data you want to set on label's text? If it is a data type use the specifiers in the string.
%i // for int
%# // for string or object adress
%c // for char
%f // for float
etc...

if statement not working

I have an app where there is a timer. It goes from 10 to 0. Using NSTimer. I have said:
.m:
//
// Skin.m
// PopThatPimple
//
// Created by Rohan Kapur on 6/2/11.
// Copyright 2011 UWCSEA. All rights reserved.
//
#import "Skin.h"
#implementation Skin
#synthesize theAudio;
#synthesize label;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint point = [myTouch locationInView:pimple];
if ( CGRectContainsPoint(pimple.bounds, point) ) {
[self checkcollision];
}
}
-(void)checkcollision {
pimple.center = CGPointMake(
random() % (unsigned int)theview.bounds.size.width,
random() % (unsigned int)theview.bounds.size.height
);
score.text = [NSString stringWithFormat:#"%d", ([score.text intValue] + 1)];
sad.hidden = NO;
NSURL* popURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"pop" ofType:#"mp3"]];
AudioServicesCreateSystemSoundID((CFURLRef) popURL, &popID);
AudioServicesPlaySystemSound(popID);
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
-(void)showActivity{
int currentTime = [time.text intValue];
int newTime = currentTime + -1;
time.text = [NSString stringWithFormat:#"%d", newTime];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)viewDidLoad {
myTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector (showActivity) userInfo:nil repeats:YES];
[label2 setHidden:YES];
[super viewDidLoad];
}
-(void)void2 {
if ([label3.text isEqualToString:#"0"]) {
[[self view] addSubview:timeup];
score.text = label2.text;
label2.hidden = NO;
}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
-(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.
}
-(void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
.h:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface Skin : UIViewController {
IBOutlet UIImageView *pimple;
IBOutlet UIImageView *smile;
IBOutlet UIImageView *sad;
AVAudioPlayer *theAudio;
IBOutlet UIView *theview;
SystemSoundID popID;
IBOutlet UIImageView *face;
IBOutlet UIImageView *face2;
IBOutlet UIImageView *face3;
IBOutlet UIImageView *face4;
IBOutlet UIImageView *face5;
IBOutlet UIImageView *face6;
IBOutlet UIImageView *face7;
IBOutlet UIImageView *face8;
IBOutlet UIImageView *face9;
IBOutlet UIImageView *face10;
IBOutlet UIImageView *face11;
IBOutlet UIImageView *face12;
IBOutlet UIImageView *face13;
IBOutlet UIImageView *face14;
IBOutlet UIImageView *face15;
IBOutlet UILabel *label2;
IBOutlet UILabel *score;
IBOutlet UIImageView *skin;
IBOutlet UILabel *time;
NSTimer * myTicker;
IBOutlet UILabel *label3;
IBOutlet UIImageView * timeup;
}
-(IBAction)start;
-(void)void2;
-(void)showActivity;
#property (nonatomic, retain) UILabel *label;
#property (nonatomic, retain)AVAudioPlayer *theAudio;
-(void)checkcollision;
#end
I have not used all of my outlets and ibactions yet. And i have not used avfoundation yet
I have CONNECTED EVERYTHING! I HAVE CHECKEED A THOUSAND TIMES! When it reaches zero nothing happens and the timer just goes to the negatives. WHat am I doing wrong?
There are two major problems here, the first is you're using an assignment operator, not a comparison operator, I'd imagine you have a warning on that.
The second is an == comparison is useless here, the fixed code would be.
if ([label3.text isEqualToString:#"0"])
I'll second (third?) what #BoltClock and #Joshua Weinberg wrote, but add that checking the text displayed in a label is a poor way to determine whether a timer has expired. At some point, somewhere in your program, you're setting the label's text to #"0", so you already know that that condition is true. Likewise with the score... your controller should know what the score is, and it definitely shouldn't be relying on a label to get it.
Don't store data in your view objects. Data should flow from your model through your controller to your view. It only flows in the opposite direction when you're accepting user input.

MySecondViewController won't display correctly

I have programmatically created a second view controller (MySecondViewController) which was all done and hooked up without using the visual object editor.
My problem is that when I run the app, only the original view controller (HelloWorldViewController) comes up; the second view controller does not show up. When I click on the view for HelloWorldViewController, nothing comes up.
Can you tell me what's wrong?
windowBasedAppAppDelegate.h
#import <UIKit/UIKit.h>
//-- Add a forward reference to the HelloWorldViewController class --
#class HelloWorldViewController;
#interface windowBasedAppAppDelegate : NSObject <UIApplicationDelegate> {
//-- create an instance of the view controller--
HelloWorldViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
//--Expose the view controller as a property--
#property (nonatomic, retain) IBOutlet HelloWorldViewController *viewController;
#end
windowBasedAppAppDelegate.m
#import "windowBasedAppAppDelegate.h"
#import "HelloWorldViewController.h"
#import "MySecondViewController.h"
#implementation windowBasedAppAppDelegate
#synthesize window;
#synthesize viewController;
//-- a second view controller object--
MySecondViewController *mySecondViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//-- add the new view to the current window--
//-- instantiate the second view controller --
mySecondViewController = [[MySecondViewController alloc]
initWithNibName:nil
bundle:nil];
// -- add the view from the second controller --
//[window addSubview:mySecondViewController.view];
[window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[MySecondViewController release];
[viewController release];
[window release];
[super dealloc];
}
#end
MySecondViewController.h
#import <UIKit/UIKit.h>
#interface MySecondViewController : UIViewController {
//-- create two outlets, label and button --
UILabel *label;
UIButton *button;
}
//-- expose the outlets as properties --
#property (nonatomic, retain) UILabel *label;
#property (nonatomic, retain) UIButton *button;
// -- declaring the IBAction ---
- (IBAction) buttonClicked: (id)sender;
#end
MySecondViewController.h
#import "MySecondViewController.h"
#implementation MySecondViewController
#synthesize label, button;
- (IBAction) buttonClicked: (id)sender {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:#"Action Invoked!" message:#"Button clicked" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
HelloWorldViewController.h
#import <UIKit/UIKit.h>
#interface HelloWorldViewController : UIViewController {
}
#end
HelloWorldViewController.m
import "HelloWorldViewController.h"
#implementation HelloWorldViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
Added code from MySecondViewController.m
//-- add the views to the view window --
[self.view addSubview:label];
[self.view addSubview:button];
[view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
self.view = view;
[super viewDidLoad];
Since the view is being initialized and that it has a proper frame, the problem could be this sequence of code,
[window addSubview:mySecondViewController.view];
[window addSubview:viewController.view];
As the viewController's view will have the same frame, it will be added over mySecondViewController's view. So you will have to remove the second line for the view to show up.
You never actually push the new view controller onto the screen.
mySecondViewController = [[MySecondViewController alloc]
initWithNibName:nil
bundle:nil];
// -- add the view from the second controller --
//[window addSubview:mySecondViewController.view];
[window addSubview:viewController.view];
You add viewController.view, (which should be undefined...), but not mySecondViewController.view.
Also, programHasFinishedLaunching may not be the best possible place to push it. If I were you, I'd put a button on the front page that pushes the new view in, it's easier that way.