UIViewController difficulty - iphone

//RootViewViewController.h
#import <UIKit/UIKit.h>
#import "SettingsViewController.h"
#import "OneSlotViewController.h"
#import "TwoSlotViewController.h"
#import "BingoSlotViewController.h"
#import "SettingsViewController.h"
#interface RootViewViewController : UIViewController{
IBOutlet UIButton *owaru;
OneSlotViewController *oneSlotViewController;
TwoSlotViewController *button2ViewController;
BingoSlotViewController *button3ViewController;
UIViewController *pushedController;
UIButton *hajimekara;
SettingsViewController *settingsVc;
}
#property (retain, nonatomic) UIButton *hajimekara;
#property (strong, nonatomic) IBOutlet UIButton *owaru;
#property (nonatomic, retain) OneSlotViewController *button1ViewController;
#property (nonatomic, retain) TwoSlotViewController *button2ViewController;
#property (nonatomic, retain) BingoSlotViewController *button3ViewController;
#property (nonatomic, retain) UIViewController *pushedController;
#property (nonatomic, retain) SettingsViewController *settingsVc;
//RootViewViewController.m
#synthesize button1ViewController;
#synthesize button2ViewController;
#synthesize button3ViewController;
#synthesize pushedController;
#synthesize settingsVc;
-(IBAction) startButtonPressed:(id) sender {
if (self.settingsVc.pushedController!=nil) {
NSLog(#"push");
[self presentViewController:self.settingsVc.pushedController animated:YES completion:NULL];
}
}
//SettingViewController.h
#import "OneSlotViewController.h"
#import "TwoSlotViewController.h"
#import "BingoSlotViewController.h"
#import "SettingsViewController.h"
#import "AGImagePickerController.h"
#class RootViewViewController;
#interface SettingsViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIPopoverControllerDelegate,UIScrollViewDelegate>{
OneSlotViewController *oneSlotViewController;
TwoSlotViewController *button2ViewController;
BingoSlotViewController *button3ViewController;
UIViewController *pushedController;
RootViewViewController *mainVc;
UIImageView *lastPriceView;
CustomImagePicker *_imagePicker;
UINavigationController *navController;
}
#property (nonatomic, retain) UIPopoverController *popoverController;
#property (nonatomic, retain) OneSlotViewController *button1ViewController;
#property (nonatomic, retain) TwoSlotViewController *button2ViewController;
#property (nonatomic, retain) BingoSlotViewController *button3ViewController;
#property (nonatomic, retain) UIViewController *pushedController;
#property (nonatomic, retain) RootViewViewController *mainVc;
#property (strong, nonatomic) IBOutlet UIImageView *lastPriceView;
#property (nonatomic, retain) CustomImagePicker *imagePicker;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
//SettingViewController.m
-(IBAction) button1Pressed:(id)sender {
if (self.button1ViewController==nil) {
button1ViewController = [[OneSlotViewController alloc] init];
}
self.pushedController = button1ViewController;
}
-(IBAction) button2Pressed:(id)sender {
if (self.button2ViewController==nil) {
button2ViewController = [[TwoSlotViewController alloc] init];
}
self.pushedController = button2ViewController;
}
-(IBAction) button3Pressed:(id)sender {
if (self.button3ViewController==nil) {
button3ViewController = [[BingoSlotViewController alloc] init];
}
self.pushedController = button3ViewController;
}
I have already declare there instances un the two controllers.
Whenever a button is pressed in the SettingsViewController it will pass the ViewController to the MainViewController's startButton.
But I cant seem to make it work. Thanks for your help.

SettingsViewController.pushedController and MainViewController.pushedController are separate variables. Changing one will not effect the other.
You have two options. You either need to store the SettingsViewController in the MainViewController, or pass the MainViewController to SettingsViewController.
If MainViewController keeps a reference to the SettingsViewController, then you can:
-(IBAction) startButtonPressed:(id) sender {
if (self.settingsViewController.pushedController!=nil) {
NSLog(#"push");
[self presentViewController:self.settingsViewController.pushedController animated:YES completion:NULL];
}
}
If SettingsViewController is passed a reference to MainViewController, then you can:
-(IBAction) button1Pressed:(id)sender {
if (self.button1ViewController==nil) {
button1ViewController = [[ViewOneController alloc] init];
}
self.mainViewController.pushedController = button1ViewController;
}
-(IBAction) button2Pressed:(id)sender {
if (self.button2ViewController==nil) {
button2ViewController = [[ViewTwoController alloc] init];
}
self.mainViewController.pushedController = button2ViewController;
}
-(IBAction) button3Pressed:(id)sender {
if (self.button3ViewController==nil) {
button3ViewController = [[ViewThreeController alloc] init];
}
self.mainViewController.pushedController = button3ViewController;
}
Pick one of these things and you should be OK.

Related

unable to set values taken from text fields from a UIViewController to another class for calculation

I am having problems unable to set values taken from UItextfields from a Viewcontroller to another class which does the calculations from those values.
#interface InitialViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *phValue;
#property (weak, nonatomic) IBOutlet UITextField *pco2Value;
#property (weak, nonatomic) IBOutlet UITextField *hco3Value;
#property (weak, nonatomic) IBOutlet UITextField *sodiumValue;
#property (weak, nonatomic) IBOutlet UITextField *chlorideValue;
#property (nonatomic,retain)NSDecimalNumber* ph;
#property (nonatomic,retain) NSDecimalNumber* pco2;
#property (nonatomic,retain)NSDecimalNumber* hco3;
#property (nonatomic,retain)NSDecimalNumber* sodium;
#property (nonatomic,retain)NSDecimalNumber* chloride;
- (IBAction)analyseValues:(UIButton *)sender;
#end
in the implementation
-(void)values{
[self setPh:[[NSDecimalNumber alloc] initWithFloat:phValue.text.floatValue ]];
[self setPco2:[[NSDecimalNumber alloc] initWithFloat:pco2Value.text.floatValue]];
[self setHco3:[[NSDecimalNumber alloc ] initWithFloat:hco3Value.text.floatValue]];
[self setSodium:[[NSDecimalNumber alloc] initWithFloat:sodiumValue.text.floatValue] ];
[self setChloride:[[NSDecimalNumber alloc] initWithFloat:chlorideValue.text.floatValue] ];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self values];}
Then I have another class which takes these values ,but on running the program it runs but does assign values taken from the initialViewController.
#import <Foundation/Foundation.h>
#import "InitialViewController.h"
#interface AcidBaseCalculations : NSObject
#property (nonatomic) float ph;
#property (nonatomic) float pco2;
#property (nonatomic) float chloride;
#property (nonatomic) float sodium;
#property (nonatomic) float hco3;
#implementation AcidBaseCalculations
#synthesize ph,pco2,chloride,hco3,sodium;
-(void)calculations{
InitialViewController *bvc = [[InitialViewController alloc] init];
ph = bvc.ph.floatValue ;
pco2 = bvc.pco2.floatValue;
// these values are not being set although the program runs successfully
hco3 = bvc.hco3.floatValue;
sodium = bvc.sodium.floatValue;
chloride = bvc.chloride.floatValue;
I want to use these new values here and perform some logical operations in this class.
You are creating a new InitialViewController with this line:
InitialViewController *bvc = [[InitialViewController alloc] init];
It is a diffrent instance than your other/original InitialViewController
You either have to make a property
#property(nonatomic,strong)InitialViewController myInitialVC;
And set this property when you are alloc/initing your AcidBaseCalculations.
Our you have to make property for all variables you would like to calculate and set them while alloc/initing to the
AcidBaseCalculations

Add object to an array does not work, when I iterate through the array I see no objects

What I want:
On the iPhone user enters few values and tap on save button.
App should store the values in an instance of VehicleClass and add it to an NSMutableArray.
Hope you can help.
ViewController.h
#import <UIKit/UIKit.h>
#import "VehicleClass.h"
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *producerTextField;
#property (weak, nonatomic) IBOutlet UITextField *modelTextField;
#property (weak, nonatomic) IBOutlet UITextField *pYearTextField;
#property (weak, nonatomic) IBOutlet UITextField *priceTextField;
#property (weak, nonatomic) NSMutableArray *carArray;
#property (weak, nonatomic) IBOutlet UILabel *statusLabel;
- (IBAction)addCar:(id)sender;
- (IBAction)showCars:(id)sender;
#end
ViewController.m
#synthesize producerTextField, modelTextField, pYearTextField, priceTextField, carArray;
- (void)viewDidLoad
{
[super viewDidLoad];
carArray = [NSMutableArray array];
}
- (IBAction)addCar:(id)sender
{
VehicleClass *newVehic = [[VehicleClass alloc]initWithProducer:producerTextField.text
andModel:modelTextField.text
andPYear:producerTextField.text
andPrice:priceTextField.text];
[carArray addObject:newVehic];
if (carArray != nil) {
statusLabel.text = #"Car saved to array.";
}
else {
statusLabel.text = #"Error, check your code.";
}
}
VehicleClass.h
#import <Foundation/Foundation.h>
#interface VehicleClass : NSObject
#property (weak, nonatomic) NSString *producer;
#property (weak, nonatomic) NSString *model;
#property (weak, nonatomic) NSString *pYear;
#property (weak, nonatomic) NSString *price;
-(id)initWithProducer:(NSString *)varProducer
andModel:(NSString *)varModel
andPYear:(NSString *)varPYear
andPrice:(NSString *)varPrice;
#end
VehicleClass.m
#import "VehicleClass.h"
#implementation VehicleClass
#synthesize model, price,pYear,producer;
-(id)initWithProducer:(NSString *)varProducer
andModel:(NSString *)varModel
andPYear:(NSString *)varPYear
andPrice:(NSString *)varPrice
{
self = [super init];
if (self != nil)
{
producer = varProducer;
model = varModel;
pYear = varPYear;
price = varPYear;
}
return self;
}
#end
Maybe the problem has something to do with my init method?
carArray is a weak property, it should be strong.
Also: you should use self.carArray and not carArray. When you use carArray, you are using the underlying iVar and not the actual property. It will cause confusion later.
Your properties should be, instead of property (weak, nonatomic), property (nonatomic, retain).
In your init method, put:
-(id)initWithProducer:(NSString *)varProducer
andModel:(NSString *)varModel
andPYear:(NSString *)varPYear
andPrice:(NSString *)varPrice
{
self = [super init];
if (self != nil)
{
self.producer = varProducer;
self.model = varModel;
self.pYear = varPYear;
self.price = varPYear;
}
return self;
}
Then, to check if your carArray has objects, instead of using if (carArray != nil), use if ([carArray count] > 0), because you already init it.
Your carArray is weak property. carArray should be strong.

Calling a function in viewcontroller from appdelegate

I want to call a function in a viewController from my appDelegate but with the following code, it doesn't get called. What am I doing wrong?
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "DetailsToTransfer.h"
#class AccountDetailTransferViewController;
#interface AccountDetailTransferAppDelegate : NSObject <UIApplicationDelegate> {
DetailsToTransfer *objDetailsToTransfer;
}
#property (nonatomic, retain) DetailsToTransfer *objDetailsToTransfer;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet AccountDetailTransferViewController *viewController;
-(void)sendToTransferScreen:(NSArray *)detailsArray;
#end
AppDelegate.m:
....
-(void)sendToTransferScreen:(NSArray *)detailsArray {
[objDetailsToTransfer setLabels:detailsArray];
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:#"DetailsToTransfer" bundle:nil];
[self.window addSubview:objDetailsToTransfer.view];
}
DetailsToTransfer.h:
#import <UIKit/UIKit.h>
#interface DetailsToTransfer : UIViewController {
NSArray *accountDetailsArray;
UILabel *nameLabel;
UILabel *accountLabel;
IBOutlet UIButton *btnTransfer;
IBOutlet UIButton *btnBack;
}
#property (nonatomic, retain) NSArray *accountDetailsArray;
#property (nonatomic, retain) IBOutlet UILabel *nameLabel;
#property (nonatomic, retain) IBOutlet UILabel *accountLabel;
-(IBAction)sendDetails;
-(IBAction)goBack;
-(void)setLabels:(NSArray *)array;
#end
DetailsToTransfer.m
....
-(void)setLabels:(NSArray *)array {
NSLog(#"Setting Labels");
self.nameLabel.text = [array objectAtIndex:0];
self.accountLabel.text = [array objectAtIndex:1];
}
....
I would like to add that all the properties have been synthesized and that I'm calling the method in the appDelegate properly (i checked using NSLogs)
In AppDelegate.m:
Looks as if you are calling a method on your object before the object has been created. Until you have alloc / init your object, there will be no labels to set text.
Try changing your method to this:
-(void)sendToTransferScreen:(NSArray *)detailsArray {
if (!objDetailsToTransfer) {
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:#"DetailsToTransfer" bundle:nil];
[objDetailsToTransfer setLabels:detailsArray];
[self.window addSubview:objDetailsToTransfer.view];
}
}
The problem might be that you are trying to set the values on a object that hasn't been created yet. I also provided an if statement to prevent the object from being created multiple times.

Hmm, getting : may not respond to '-presentModalViewController:animated:'

I`m getting this annoying warning :
warning: 'AppDelegate' may not respond to '-presentModalViewController:animated:'
In this implementation file :
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
at the line :
self presentModalViewController:controller animated:YES];
and here is the header file :
#import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate, FlipsideViewControllerDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
UIImageView *infoButton;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
#property (nonatomic, retain) IBOutlet UIView *flipside;
#property (nonatomic, retain) IBOutlet UIImageView *infoButton;
#property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)showInfo:(id)sender;
- (IBAction)changePage:(id)sender;
#end
Obviously there is something I`m not getting, but I would appreciate if someone could help :)
You must persent modal view controllers from an instance of UIViewController (not your application delegate). If you check out the documentation on UIViewController, you can access a sample application using the modal presentation.

Class Delegate does not implement protocol

I`m doing something wrong here but I can't figure out what it is .
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
#property (nonatomic, retain) IBOutlet UIView *flipside;
#property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)showInfo:(id)sender;
- (IBAction)changePage:(id)sender;
#end
AppDelegate.m
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
This is where I`m getting :
warning: class 'AppDelegate' does not implement the 'FlipsideViewControllerDelegate' protocol.
After line :
controller.delegate = self;
My FlipsideViewController.h looks like this :
#import <UIKit/UIKit.h>
#protocol FlipsideViewControllerDelegate;
#interface FlipsideViewController : UIViewController {
id <FlipsideViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#en
Any help would be greatly appreciated :)
It's exactly what the error message says. AppDelegate just doesn't implement the protocol. In your header file, add FlipsideViewControllerDelegate between the brackets (i.e. <UIApplicationDelegate, UIScrollViewDelegate, FlipsideViewControllerDelegate>), and implement the -flipsideViewControllerDidFinish: method.
try adding FlipsideViewControllerDelegate to the appDelegate
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate,FlipsideViewControllerDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}