2012-04-18 41 views
0

首先感謝我的英語。Xcode 4:我如何在兩個不同的類中使用int?

我正在創建一個應用程序,用於統計您在按鈕內部觸摸的次數。該應用程序被UITabBarController分割:在第一個視圖(類:ViewController)中有一個按鈕和一個顯示「int」(int counter;)值的標籤,問題是我想設置一個最大數量,所以當你點擊50次按鈕時,會出現一個alertView。我想讓這個最大數字更靈活,這樣您就可以通過tabBar第二個視圖中的滑塊進行調整。

下面是代碼:

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController{ 
    int counter; 
    IBOutlet UILabel *counterLabel; 
} 

-(IBAction)counter:(id)sender; 
-(IBAction)Reset:(id)sender; 

@end 

ViewController.m

#import "ViewController.h" 

@implementation ViewController 

-(IBAction)counter:(id)sender{ 
    if (counter < end) // in spite of end, I want to put Max (declared in AjustesView.h) 
    { 
     counter = counter+1; 
     counterLabel.text = [NSString stringWithFormat:@"%i", Jugador1]; 

    }else 
    { 
     UIAlertView *alerta = [[UIAlertView alloc] initWithTitle:@"Alerta!" message:@"Ha llegado a 100 cliks" delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil, nil]; 
     [alerta show]; 
     [alerta release]; 
    } 
} 

-(IBAction)Reset:(id)sender{ 
    counter = 0; 
    counterLabel.text = [NSString stringWithFormat:@"%i", counter]; 
} 

AjustesView.h

@interface AjustesView : UIViewController{ 
    IBOutlet UISlider *maxSlider; 
    IBOutlet UILabel *maxLabel; 
    int max; 
} 

@property (nonatomic, retain) IBOutlet UISlider *maxSlider; 

-(IBAction)AdjustmentOfMaximum:(id)sender; 

@end 

AjustesView.m

#import "AjustesView.h" 

@implementation AjustesView 

@synthesize maxSlider; 

-(IBAction)AdjustmentOfMaximum:(id)sender{ 
    max = self.maxSlider.value; 
    maxLabel.text = [NSString stringWithFormat:@"%i", max]; 
} 

IB文件的.xib不是故事情節 感謝您的時間!

回答

0

你可以做的一件事是將你的int值存儲在你的應用程序的AppDelegate中,並將它合成爲一個屬性。每當你想更新或獲得的價值,你只需要從AppDelegate中獲得的價值,像這樣

#include "AppDelegate.h" 



.. 

//whenever you want to access the value or update the value 
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
appDelegate.myCounter = newvalue; 
+0

將它放在我的情況下工作嗎? ,因爲newValue等於第二個視圖中的滑塊的值 – yeker3 2012-04-18 18:36:23

0

這聽起來像你想有一個用戶定義的偏好。爲此,請使用NSUserDefaults

使用以下方法設置並從NSUserDefaults獲取整數。

-(void)setInt:(int)myInt{ 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    //To set a value 
    [prefs setInteger:myInt ForKey:@"myInt"]; 
    [prefs syncronize]; 
} 

-(int)getInt{ 
//to get a value 
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"myInt"]; 
} 

This is the Apple NSUserDefaults Class Reference

+0

我應該在哪裏寫每個東西?我不是高級程序員。 thx – yeker3 2012-04-21 18:53:28

+0

對不起,但我不知道該怎麼做:( 我正在寫它在視圖controller.m(看我的帖子) 感謝和抱歉,其他時間 – yeker3 2012-04-22 21:58:46

相關問題