在文本字段中自动增加数字

时间:2013-10-17 11:41:28

标签: iphone ios uitextfield

大家好

我正在生成一个我正在4 fields ( product id, product name, product price, product description)的应用程序。

现在我想要保存产品ID 在我保存数据时自动生成,即当我运行我的应用时,应该自动生成产品ID的文本字段,当我点击保存按钮时这个id应该加1,并且应该保存最后数据的id。

请帮助我。

3 个答案:

答案 0 :(得分:0)

试试这个......

//fetch the MAX id
[self databaseOpen];
NSString *cc=[NSString stringWithFormat:@"Select MAX(ProductID) from Table"];
NSMutableArray *temp=[[NSMutableArray alloc]init];
temp=[[database executeQuery:cc]mutableCopy];
[database close];

解释我从数据库表中获取Maximum product_id。

int Product_id;
if (temp.count!=0 && ([[temp objectAtIndex:0]valueForKey:@"MAX(ProductID)"] !=[NSNull null]))
    Product_id=[[[temp objectAtIndex:0]valueForKey:@"MAX(ProductID)"]intValue]+1;
else
    Product_id=1;
//fetch the MAX id

解释如果表中没有记录,则Product_id = 1                  否则它会增加1;

  Txt_productID.text=[NSString stringwithFormat:@"%d",Product_id];

解释在文本字段中打印Product_id

[self databaseOpen];
NSString *q=[NSString stringWithFormat:@"Insert into Table (NAme,Price,Description) values ('%@','%@','%@')",txt_name.text,txt_price.text,txt_des.text];
    [database executeNonQuery:q];
    [database close];

解释将新记录保存在数据库中。

答案 1 :(得分:0)

你必须看看这个教程

http://upadhyayajayiphone.blogspot.in/2012/11/insert-record-in-sqlite-table-get.html

http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application

它包含您的所有操作。在获取数据时,使用SELECT MAX(product_id) from TABLE_NAME来获取最后的数据。

答案 2 :(得分:0)

如果你想通过NSUserDefaults获得,请使用以下代码

// get total count   
int count=[[NSUserDefaults standardUserDefaults]integerForKey:@"TotalIdCount"];


// on save button click increment count by one and stored it
count=count+1;
[[NSUserDefaults standardUserDefaults ] setInteger:count forKey:@"TotalIdCount"];
[[NSUserDefaults standardUserDefaults] synchronize];