簡體   English   中英

如何從單例獲取UITextFields

[英]How to obtain UITextFields from a singleton

我只需要四個參數即可在三個視圖控制器中驅動計算。 參數來自第一個視圖中的四個UITextField。 我沒有使用全局變量,而是開發了一個單例類來使參數可用於每個控制器。 我可以在控制器中引用變量OK,但是我不知道如何初始化它們。 我已經通過用IB開發的窗口從第一個viewcontroller中獲取了變量,但是似乎找不到在單例中初始化這些變量的方法。

非常感謝您的幫助和指導。

//
//  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

這里有很多事情我會改變。 首先,我不會創建“ Globals”類。 其次,如果我這樣做了,就永遠不要將UIView對象存儲在設計為“模型”的類中。 第三,如果我絕對需要實現單例,那么我將遵循Cocoa的設計模式

我只需要四個參數即可在三個視圖控制器中驅動計算。

在這一點上,我建議您完全放棄此類,並重新考慮您的設計問題。 是否僅需要存儲四個NSString參數(或NSNumberNSIntegerfloat或...)? 如果是這樣,將參數存儲在視圖類中完全是矯kill過正。 只需創建一個簡單的類來存儲參數,其標題將類似於:

@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

我將在我的應用程序委托中或在根視圖控制器中實例化此類的對象,然后將其傳遞給需要訪問它的任何其他視圖控制器。 最后,我將使用鍵值觀察,以便每個需要它的視圖控制器都將自動更新對參數的任何更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM