开发者

How to obtain UITextFields from a singleton

开发者 https://www.devze.com 2023-01-03 15:35 出处:网络
I need just four parameters to drive the calculations in three view controllers. The parameters come from four UITextFields in the first view. I\'m not using global variables but have developed a sing

I need just four parameters to drive the calculations in three view controllers. The parameters come from four UITextFields in the first view. I'm not using global variables but have developed a singleton class to enable the parameters to be available to each controller. I can reference the variables OK in the controllers but I don't know how to initialize them. I've obtained the variables from the first viewcontroller through a windows I developed with IB but can't seem to find a way to get these initialized in the singleton.

Help and guidance gratefully appreciated.

//
//  GlobalParameters.h
//  ProjectEstimator
//
//  This 开发者_高级运维is a SINGLETON class used to handle global parameters for use in the various view controllers.
//
//
//
//  Created by Frank Williamson on 10/06/2010.
//

#import <Foundation/Foundation.h>

@interface GlobalParameters : NSObject {

// Place any "global" variables here

//  float *processes;
//  float *entities;
//  float *transactions;
//  float *users;



    IBOutlet UITextField *noOfProcesses;
    IBOutlet UITextField *noOfEntityClusters;
    IBOutlet UITextField *noOfTransactions;
    IBOutlet UITextField *noOfUserArea;

}

@property (retain, nonatomic) UITextField *noOfProcesses;
@property (retain, nonatomic) UITextField *noOfEntityClusters;
@property (retain, nonatomic) UITextField *noOfTransactions;
@property (retain, nonatomic) UITextField *noOfUserArea;

// message from which our instance is obtained    
+ (GlobalParameters *)sharedInstance;    
@end

//
//  GlobalParameters.m
//  ProjectEstimator        Singleton for handling (global) parameters.
//
//  Created by Frank Williamson on 10/06/2010.
//

#import "GlobalParameters.h"


@implementation GlobalParameters;

@synthesize     noOfProcesses;
@synthesize     noOfEntityClusters;
@synthesize     noOfTransactions;
@synthesize     noOfUserArea;

+ (GlobalParameters *)sharedInstance{

    // the instance of this class is stored here        
    static GlobalParameters *noOfProcesses = nil;
    static GlobalParameters *noOfEntityClusters = nil;
    static GlobalParameters *noOfTransactions = nil;
    static GlobalParameters *noOfUserArea = nil;

    // check to see if an instance already exists

    if (nil == noOfProcesses) {
        noOfProcesses  = [[[self class] alloc] init];

        // **How to I initialize UITextFields from a ViewController in here??**
    }
    // return the instance of this class
    return noOfProcesses;

    if (nil == noOfEntityClusters) {
        noOfEntityClusters  = [[[self class] alloc] init];

        // **How to I initialize UITextFields from a ViewController in here??**
    }
    // return the instance of this class
    return noOfEntityClusters;  

    if (nil == noOfTransactions) {
        noOfTransactions  = [[[self class] alloc] init];

        // **How to I initialize UITextFields from a ViewController in here??**
    }
    // return the instance of this class
    return noOfTransactions;

    if (nil == noOfUserArea) {
        noOfUserArea  = [[[self class] alloc] init];

        // **How to I initialize UITextFields from a ViewController in here??**
    }
    // return the instance of this class
    return noOfUserArea;        
}
@end


There's a number of things here I would change. First, I wouldn't create a "Globals" class. Second, if I did, would never store UIView objects in class designed to be a "model". Third, if I absolutely needed to implement a singleton, I would follow the design pattern for Cocoa.

I need just four parameters to drive the calculations in three view controllers.

And at that point I recommend you throw out this class entirely and re-think your design problem. Is it simply that you need to store four NSString parameters (or NSNumbers or NSIntegers or floats or...)? If so, it's complete overkill to store the parameters inside a view class. Just create a simple class to store the parameters, whose header would look something like:

@interface MyParameters : NSObject
{
    NSString* someString;
    NSNumber* someNumber;
    NSInteger someInteger;
    float     someFloat;
}

@property (nonatomic, retain) NSString* someString;
@property (nonatomic, retain) NSNumber* someNumber;
@property (nonatomic, assign) NSInteger someInteger;
@property (nonatomic, assign) float     someFloat;

@end

I would instantiate an object of this class either in my application delegate or in my root view controller, and then I would pass it along to any other view controllers that needed access to it. Finally, I would use Key-Value Observing so every view controller that needs it will get automatic updates to any changes to the parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消