Iphone 如何从单例获取UITextFields

Iphone 如何从单例获取UITextFields,iphone,Iphone,我只需要四个参数来驱动三个视图控制器中的计算。参数来自第一个视图中的四个UITextField。我没有使用全局变量,但已经开发了一个单例类,使每个控制器都可以使用参数。我可以在控制器中引用变量OK,但我不知道如何初始化它们。我已经通过我用IB开发的windows从第一个viewcontroller获得了变量,但似乎找不到在singleton中初始化这些变量的方法 非常感谢您的帮助和指导 // // GlobalParameters.h // ProjectEstimator // // T

我只需要四个参数来驱动三个视图控制器中的计算。参数来自第一个视图中的四个UITextField。我没有使用全局变量,但已经开发了一个单例类,使每个控制器都可以使用参数。我可以在控制器中引用变量OK,但我不知道如何初始化它们。我已经通过我用IB开发的windows从第一个viewcontroller获得了变量,但似乎找不到在singleton中初始化这些变量的方法

非常感谢您的帮助和指导

//
//  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
//
//全球参数
//项目估计员
//
//这是一个单例类,用于处理各种视图控制器中使用的全局参数。
//
//
//
//由弗兰克·威廉姆森于2010年6月10日创作。
//
#进口
@接口全局参数:NSObject{
//在此处放置任何“全局”变量
//浮动*进程;
//浮动*实体;
//浮动*交易;
//浮动*用户;
IBOutlet UITextField*noOfProcesses;
IBOutlet UITextField*noOfEntityClusters;
IBOUTLE UITextField*noOfTransactions;
IBOutlet UITextField*noOfUserArea;
}
@属性(保留,非原子)UITextField*noOfProcesses;
@属性(保留,非原子)UITextField*noOfEntityClusters;
@属性(保留,非原子)UITextField*noOfTransactions;
@属性(保留,非原子)UITextField*noOfUserArea;
//从中获取实例的消息
+(全球参数*)共享状态;
@结束
//
//全球参数
//处理(全局)参数的ProjectEstimator单例。
//
//由弗兰克·威廉姆森于2010年6月10日创作。
//
#导入“GlobalParameters.h”
@实施全球参数;
@合成noOfProcesses;
@合成noofentyclusters;
@合成正反变换;
@合成木霉素;
+(全球参数*)共享状态{
//这个类的实例存储在这里
静态全局参数*NOOFPROCESS=nil;
静态全局参数*noOfEntityClusters=nil;
静态全局参数*noOfTransactions=nil;
静态全局参数*noOfUserArea=nil;
//检查实例是否已存在
if(nil==noOfProcesses){
noOfProcesses=[[self class]alloc]init];
//**如何在此处从ViewController初始化UITextFields**
}
//返回该类的实例
返回noOfProcesses;
if(nil==noOfEntityClusters){
noOfEntityClusters=[[self class]alloc]init];
//**如何在此处从ViewController初始化UITextFields**
}
//返回该类的实例
返回noOfEntityClusters;
if(nil==noOfTransactions){
noOfTransactions=[[self class]alloc]init];
//**如何在此处从ViewController初始化UITextFields**
}
//返回该类的实例
返回正午交易;
if(nil==noOfUserArea){
noOfUserArea=[[self-class]alloc]init];
//**如何在此处从ViewController初始化UITextFields**
}
//返回该类的实例
返回noOfUserArea;
}
@结束

这里有很多事情我会改变。首先,我不会创建一个“Globals”类。其次,如果我这样做了,我将永远不会在设计为“模型”的类中存储
UIView
对象。第三,如果我真的需要实现一个单例,我会遵循Cocoa的规则

我只需要四个参数来驱动三个视图控制器中的计算

在这一点上,我建议你完全抛开这门课,重新思考你的设计问题。您是否只需要存储四个
NSString
参数(或
NSNumber
s或
NSInteger
s或
float
s或…)?如果是这样的话,将参数存储在视图类中就太过分了。只需创建一个简单的类来存储参数,其标题如下所示:

@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

我将在应用程序委托或根视图控制器中实例化此类的对象,然后将其传递给需要访问它的任何其他视图控制器。最后,我将使用它,以便每个需要它的视图控制器都能自动更新参数的任何更改。

使用“101010”按钮格式化代码块和,如果(nil==noOfEntityClusters)执行将如何达到