在第二个视图控制器中编辑 UITableView 中的文本

发布于 2024-11-28 13:39:22 字数 568 浏览 0 评论 0原文

我有一个静态表视图(FirstViewController),有 3 行,每行都有自己的部分。前两个单元格中有 UITextFields,当用户点击它们或单元格时可以编辑它们。最后一个单元格有一个 UILabel,当点击它时会推动 SecondViewController,其中包含一个 UITextField。当用户按下时,UILabel 的值需要是 UITextField 的值。

如果我在 SecondViewController 上创建一个委托属性(分配),并将其设置为 FirstViewController,那么如何保证 FirstViewController 仍位于内存中而不是空?据我了解,一旦视图控制器不是最顶层的视图控制器(可见的),它就可以被释放。那么,如果设备内存不足并释放 FirstViewController 时会发生什么,那么当用户按下返回方法时,将不会发送返回方法,因为 delegate 将为 nil,之后会生成一个新的实例FirstViewController 将被创建并弹出到屏幕上,而不从 SecondViewController 接收值。

我不想在 AppDelegate 中使用“全局”变量,因为我个人认为这有点混乱。

I have a static table view (FirstViewController), with 3 rows, each in their own section. the first two cells have UITextFields in them, which are editable when the user taps on them or the cell. The last cell has a UILabel, which when tapped pushes SecondViewController, which contains a UITextField. When the user presses back the value of the UILabel needs to be the value of the UITextField.

If I create a delegate property (assign) on the SecondViewController, which is set to FirstViewController, what guarantee is there that FirstViewController will still be in memory and not nill? As I understand it as soon a view controller is not the top most view controller (the visible one) it can be deallocated. So what would happen if the device runs out of memory, and deallocates the FirstViewController, then when the user presses back the return method will not be sent as delegate will be nil, and after that a new instance of FirstViewController will be created and popped onto the screen, without receiving the value from the SecondViewController.

I don't want to use a "global" variable in the AppDelegate, as I personally think that's a bit messy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

豆芽 2024-12-05 13:39:23

您可以创建一个 Data 类,在其中可以设置变量或数组的属性(用于在 UITableView 中显示数据)。在数据类中实现一个类方法,用于检查该对象是否已实例化。如果没有,它就会这样做。它是这样的:

//DataClass.h    

@interface DataClass : NSObject {  

NSMutableArray *nameArray;  
NSMutableArray *placeArray;     

}  
@property(nonatomic,retain)NSMutableArray *nameArray;  
@property(nonatomic,retain)NSMutableArray *placeArray;  
+(DataClass*)getInstance;  
@end  


//DataClass.m  
@implementation DataClass  
@synthesize nameArray;  
@synthesize placeArray;  
static DataClass *instance =nil;  
+(DataClass *)getInstance  
{  
    @synchronized(self)  
    {  
        if(instance==nil)  
        {  

            instance= [DataClass new];  
        }  
    }  
    return instance;  
}  

现在在您的视图控制器中,您需要将此方法调用为:

DataClass *obj=[DataClass getInstance];

并使用数组。
这样您就可以在不干扰 AppDelegate 的情况下分配数据,这是一个很好的做法。

You can create a Data class where you can set the properties of variables or arrays (for displaying data in UITableView). Implement a class method in data class which checks that object has been instantiated or not. If not, it does that. It is something like this :

//DataClass.h    

@interface DataClass : NSObject {  

NSMutableArray *nameArray;  
NSMutableArray *placeArray;     

}  
@property(nonatomic,retain)NSMutableArray *nameArray;  
@property(nonatomic,retain)NSMutableArray *placeArray;  
+(DataClass*)getInstance;  
@end  


//DataClass.m  
@implementation DataClass  
@synthesize nameArray;  
@synthesize placeArray;  
static DataClass *instance =nil;  
+(DataClass *)getInstance  
{  
    @synchronized(self)  
    {  
        if(instance==nil)  
        {  

            instance= [DataClass new];  
        }  
    }  
    return instance;  
}  

Now in your view controller you need to call this method as :

DataClass *obj=[DataClass getInstance];

And use the arrays.
This way you can assign data without disturbing AppDelegate, which is a good practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文