How to change page with UISlider from an UIView - ios

To summarize my app, it's like an iBook : I have multiple pages filled in with an xml file (text and image).
Like in the iBook app, I want to use a UISlider to select and change page.
I use a UIViewController (appViewController) as contener of the different UIViews (contentViewController)
header of AppViewController :
#import <UIKit/UIKit.h>
#import "contentViewController.h"
#interface AppViewController : UIViewController <UIPageViewControllerDataSource>
{
UIPageViewController *pageController;
NSArray *pageContent;
NSDictionary *pageFields;
}
in AppViewController.m, I have :
// Create a new view controller and pass suitable data.
contentViewController *dataViewController = [[contentViewController alloc]initWithNibName:#"contentViewController" bundle:nil];
The header of the contentViewController is :
#import <UIKit/UIKit.h>
#class DCIEquipment;
#interface contentViewController : UIViewController
#property (nonatomic,strong) DCIEquipment *eqt;
#property (assign, nonatomic) NSUInteger pageIndex;
#property (assign, nonatomic) NSUInteger pageNumber;
#property (strong, nonatomic) IBOutlet UISlider *pageSlider;
#end
Because in iBook, the slider seems to follow the curling movement of the page, I put my UISlider on the contentViewController. Everything works good with the UISlider but I don't how know to do to informe the UIViewController to change the page (I assumed that the code of changing the page has to be done in this controller).
Please tell me if you need some more code. I hope that explainations are clear. I juste want to know where to put the UISlider (which view) and how to send value to the pageViewController.
Thank you in advance.
Raphael

Use the protocols. There are lots of examples. And you are using at least one of them (UIPageViewControllerDataSource).
Just create one for your contentViewController (in it's header file)
#class ContentViewController; // this is fast-forward declaration
#protocol ContentViewControllerDelegate
- (void)contentViewController:(ContentViewController *)controller didChangePage:(NSUInteger)pageIndex;
#end
#interface ContentViewController : UIViewController
// ...
#property (weak,nonatomic) id<ContentViewControllerDelegate> delegate; // don't forget to set the value with your appViewController
// ...
#end
In your ContentViewController-implementation file in some method just call the delegate method
if (self.delegate && [self.delegate respondsToSelector:#selector(contentViewController:didChangePage:)])
[self.delegate contentViewController:self didChangePage:newIndex];
And don't forget to implement this method in appViewController class.

Related

Second view controller has no data

I have the delegate method I implement to pass data from FirstViewController to SecondViewController.
First, I created a text Field and Button in FirstViewController. Second, I created a label to display the data in SecondViewController.
How it should work:
The user will press the button in FirstViewController and it will pass the data to the secondViewController.In addition,the user will be using a UISwipeGestureRecognizer to navigate to the second view controller, so there's no need to use the button to go to the second view controller.
How I test the application
1- I run the application. --> 2- I type "Hello" in Text field --> 3- I press the button --> 4- I swipe to the second view controller to see the data --> 5- I don't see any data in the label ??
First i created the delegate protocol
FirstViewController.h
#class FirstViewController;
#protocol FirstControllerDelegate
-(void) updateLabel:(NSString*)string
#end
#property (weak, nonatomic) id<FirstViewControllerDelegate>delegate;
#property (weak, nonatomic) IBOutlet UITextField *TextField;
- (IBAction)button:(id)sender;
FirstViewController.m
#interface FirstViewController. ()
#end
#implementation ViewController1
#synthesize delegate;
#synthesize TextField;
-(void)viewDidLoad{
[super viewDidLoad];
}
- (IBAction)Button:(id)sender {
ViewController2 *vc2 = [[ViewController2 alloc] init];
vc2.stringfromTextfield1 = self.TextField.text;
}
SecondViewController.h
#import "SecondViewController.h"
#import "FirstViewController.h"
#interface SecondViewController : UITableViewController<FirstControllerDelegate>
#end
#property (weak, nonatomic) IBOutlet UILabel *Label;
#property (strong, nonatomic) NSString *stringfromTextfield1;
SecondViewController.m
#interface SecondViewController. ()
#end
#implementation SecondViewController
-(void)viewDidLoad{
[super viewDidLoad];
self.label.text = self.stringfromTextfield1;
}
I appreciate your time and effort to help me out
You can use Singleton class for this, or you can use extern NSString for move the string value one to second View Controller.
in first view controller.
extern NSString *MyStr;
in the #interface define it.
#interface{
NSString *MyStr;
}
In the #Implementation assign the value what you want.
and the second view controller just #import the first view and use,
MyStr As a variable and its having the value also from first view controller.
This thing worked for me.
Or simply add an AppDelegate property to store the variable in both ViewControllers:
AppDelegate *appDel=[[UIApplication sharedApplication] delegate];
appDel.variable=#"text";
You can save data in App delegate and access it across view controllers in your application.You have to create a shared instance of app delegate.
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
like this
Suppose you declare a NSString object *strthen you can access it in any view controller by appDelegate.str

Duplicate interface definition for class "ViewController"

At the last line, I got the error: Duplicate interface definition for class "ViewController". I want to do an IBAction. What is the fault? What can I do? Please help me.
//
// ViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface SimpleEmailViewController : UIViewController <MFMailComposeViewControllerDelegate>
- (IBAction)showEmail:(id)sender;
#end
#interface ViewController : UIViewController <MFMessageComposeViewControllerDelegate> {
}
-(IBAction)sendMessage:(id)sender;
- (IBAction)showEmail:(id)sender;
#property (nonatomic, strong) IBOutlet UIWebView *site ;
- (IBAction)call:(id)sender;
#property (retain, nonatomic) IBOutlet UIButton *myBotton;
#end
#interface ViewController : UIViewController**i**
The problem is exactly what the error states. You defined #interface viewController twice. Change the name of one to something else. As a side note, it is a terrible idea to name something with a name apple has already used. You should change both viewControllers to something else, more descriptive of what it does, like mailViewController or setupViewController. Weird stuff can happen when you use apple defined names.
#interface FSMainiPadViewController : UIViewController
Why don't you just make it simple like this?
// ViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface ViewController : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
// Actions
- (IBAction)showEmail:(id)sender;
- (IBAction)sendMessage:(id)sender;
- (IBAction)call:(id)sender;
// Properties
#property (nonatomic, strong) IBOutlet UIWebView *site;
#property (retain, nonatomic) IBOutlet UIButton *myBotton;
#end

Inheritance on view controller Objective c

I am trying to create a generic view controller and 2 other views which both will inherit from.
Now this is the generic view (what i think is relevant - if something else is needed i will add it):
#interface CardGameViewController ()
#property (strong, nonatomic) IBOutlet UITabBarItem *TabSelection;
#property (strong, nonatomic) CardMatchingGame *game;
#property (strong, nonatomic) IBOutlet UIButton *resetButton;
#property (weak, nonatomic) IBOutlet UILabel *scoreLable;
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
#end
#implementation CardGameViewController
- (CardMatchingGame*)game
{
if(!_game) _game = [[CardMatchingGame alloc]initWithCardCount:[self.cardButtons count] usingDeck:[self createDeck] gameMode:[self getActiveTabWithString:self.TabSelection.description]];
return _game;
}
- (Deck*)createDeck //abstract
{
return nil; <------- Relevant generic function
}
and these are the 2 files which inherit the mentioned file:
SetGameViewController.h:
#import "CardGameViewController.h"
#interface SetGameViewController : CardGameViewController
#end
SetGameViewController.m:
#import "SetGameViewController.h"
#import "SetCardDeck.h"
#interface SetGameViewController ()
#end
#implementation SetGameViewController
-(Deck*)createDeck
{
return [[SetCardDeck alloc]init];
}
#end
and the second one:
PlayingCardGameViewController.h:
#import "CardGameViewController.h"
#interface PlayingCardGameViewController : CardGameViewController
#end
PlayingCardGameViewController.m:
#import "PlayingCardGameViewController.h"
#import "PlayingCardDeck.h"
#interface PlayingCardGameViewController ()
#end
#implementation PlayingCardGameViewController
- (Deck*)createDeck
{
return [[PlayingCardDeck alloc]init];
}
#end
I also have tab bar navigator to switch between the 2 views.
The thing is, the first (SetGameViewController) is never instantiated no matter what i do,
Meaning the createDeck function is never called,
While the second one (PlayingCardGameViewController) is ok every time.
What i have tried:
placing break points on the main view - (CardMatchingGame*)game function.
this one only gets called when i am trying to start the second working view, and not the bad one.
If there is anything missing to give a certain lead, please let me know and i will add it.
Set breakpoints on the empty createDeck method in the base class, as well as on the method in your two subclasses.
My guess is that when you set up your tab bar controller in IB (Interface Builder) , you made one of the tabs be an instance of the generic CardGameViewController instead of a SetGameViewController.

Segmented Control- Change label text in another view

I have 2 views 1) ViewController and 2)ViewController2.
In the first view controller I have some lablels that when I run the segment controller the names change. All I want to do is, in (Viewcontroller2) where I have a label, I want it to be able to change the text as well based on the segmented controller being 0 or 1. Here is what I have.
// ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UISegmentedControl *Lang;
IBOutlet UIButton *Run;
IBOutlet UILabel *bet;
}
-(IBAction)ChangeLang:(id)sender;
#end
Then in the m file i have the following to change the data
// ViewController.m
#import "ViewController.h"
#import "ViewController2.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)ChangeLang :(id)sender {
if (Lang.selectedSegmentIndex==0)
{
bet.text=#"Apple";
Run.titleLabel.text=#"Banana";
//not sure here what code to put in order to make the label in viewcontroller2 change.
}
if (Lang.selectedSegmentIndex==1) {
bet.text=#"ORANGES";
Run.titleLabel.text=#"FRUITS";
//not sure here what code to put in order to make the label in viewcontroller2 change.
}
}
This runs fine... heres what I have in VIEWController 2
// Viewcontroller2.h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
IBOutlet UILabel *Nextword;
#end
In the M controller i just have this
#import "ViewController2.h"
#import "ViewController.h"
#interface ViewController2 ()
#end
How would I, when I run the segmented controller, make the uilabel text change in the ViewController2? Can anyone help? I dont want the screen to go to viewcontroller automatically, I was able to do that. I want the text to change, and then when i swipe to go there, i want the text in Viewcontroller2 to be according to the if statement based on the segmented controller.
Thank you
You need to have an access to instance of ViewController2 from ViewController.
Use this code inside your ViewController.h file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UISegmentedControl *Lang;
IBOutlet UIButton *Run;
IBOutlet UILabel *bet;
ViewConstroller2 * VC2;
}
-(IBAction)ChangeLang:(id)sender;
#end
When you're creating instance of ViewController2 store it in VC2 variable.
Then use this code as your UISegmentedControl change event:
-(IBAction)ChangeLang:(id)sender {
if (Lang.selectedSegmentIndex==0)
{
bet.text=#"Apple";
Run.titleLabel.text=#"Banana";
VC2.Nextword= "YOUR_WORD";
}
if (Lang.selectedSegmentIndex==1) {
bet.text=#"ORANGES";
Run.titleLabel.text=#"FRUITS";
VC2.Nextword= "YOUR_WORD";
}
}

merge iAD within another view / definition

i am having trouble merging iAD into my current code/definition. i already have code set for a card scroll view style display and wanted to add iAD to the same view controller:
how can i do that without getting the errors i am getting?
this is the code for card scroll view in view controller.h
#interface ViewController1 : UIViewController <CardScrollViewDelegate>
#end
this is my code for iAD for view controller.h but should be merged with the above otherwise i get another error saying duplicate definition:
#interface ViewController1 : UIViewController <ADBannerViewDelegate> {
SLComposeViewController *mySLComposeSheet;
}
#property (weak, nonatomic) IBOutlet ADBannerView *banner1;
The view controller name / custom class im trying to add them both to is ViewController1
UPDATE:
its much simpler than i thought, just add a comma (FOR THOSE WHO ARE CURIOUS LIKE I WAS)
#interface ViewController1 : UIViewController <CardScrollViewDelegate, ADBannerViewDelegate> {
}
#property (weak, nonatomic) IBOutlet ADBannerView *banner1;
#end
Your code and/or question is ambiguious. I see 3 view controllers and 2 delegates. Eitherway...which viewcontroller are you trying to add it to? Whichever one it is. Inside the implementation file should be the following:
Remember to import iAd and your code should look like this:
#import <iAd/iAd.h>;
#interface ViewController1 : UIViewController <ADBannerViewDelegate>
{
ADBannerView *banner1;
BOOL bannerIsVisible;
}
#property (nonatomic,assign) BOOL bannerIsVisible;
Then, in viewDidLoad you can load it in a frame etc...

Resources