2011-05-28 50 views
1

我使用核心數據和sqlite數據庫來存儲一些數據。有一個名爲entity.amount的屬性,我希望每次觸摸UIButton時將值增加1。每次觸摸UIButton時,將值遞增1

我在想我可能需要一個for循環,但無法弄清楚涉及到核心數據實體的語法。

感謝您的任何幫助。

+0

的可能重複的[(IBAction爲)按鈕標籤輸出(http://stackoverflow.com/questions/4755784/ibactionbutton-to-label-output)或[遞增一個號碼和顯示標籤上的步驟](http://stackoverflow.com/questions/237621/obj-c-incrementing-a-number-and-showing-steps-on-a-cocoa-label) – 2011-05-28 22:03:38

回答

3
// if you have your button in a view controller 

- (void)viewDidLoad { 
    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside]; 
} 

- (void)buttonPress:(id)sender { 
    //get your core data entity and call it something like model 

    model.amount++; 
    [managedObjectContext save:error]; 
} 
0

A for循環?不,您需要在該按鈕上有一個操作方法,並且在該操作方法中,您只需增加該值即可。就這樣。順便說一句,如果您使用Xcode 4,您可以通過拖放按鈕從字面上創建操作方法。請參閱下面的鏈接中的圖4.10和11。

Xcode 4 Transition Guide

+0

感謝您的回覆。我已經有了UIButton和IBAction。這是我需要幫助的增量代碼。有關如何做到這一點的任何提示?謝謝 – hanumanDev 2011-05-28 21:45:00

+1

特拉維斯有正確的想法(儘管我會以不同的方式做):控制器中的模型對象的屬性得到遞增是所有這一切。然後保存那個對象。 – Rob 2011-05-28 22:08:26