سؤال

في تطبيق iPhone الخاص بي ، لديّ Appsettings.Plist. هذا يسمح لي ، ولكن أيضا الآخرين لتغيير بعض المعلمات. واحدة من المعلمات هو اللون السائد للتطبيق. يبدو أن .plist مثل هذا:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>Red</key>
 <integer>255</integer>
 <key>Green</key>
 <integer>123</integer>
 <key>Blue</key>
 <integer>124</integer>
 <key>compositeRGB</key>
</dict>
</plist>

في الكود الخاص بي ، قرأت هذا الملف ، وأحاول جعل uicolor من هذه الأرقام الثلاثة. يجب أن أعترف أنني لا أعرف الكثير عن cgfloats ، وأظن أن هذا هو سبب مشكلتي. هذا ما افعله:

-(void)readAppSettings
{
 NSString *path = [[NSBundle mainBundle] bundlePath];
 NSString *finalPath = [path stringByAppendingPathComponent:@"appSettings.plist"];
 NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];

 unsigned int RedComponent = [[plistDictionary objectForKey:@"Red"]intValue];
 unsigned int GreenComponent = [[plistDictionary objectForKey:@"Green"]intValue];
 unsigned int BlueComponent = [[plistDictionary objectForKey:@"Blue"]intValue];

 appColor = [UIColor colorWithRed: ((float) RedComponent / 255.0f) 
          green: ((float) GreenComponent / 255.0f) 
        blue:((float) BlueComponent / 255.0f)
          alpha:1.0f];
}

كلما حاولت استخدام appColor بصفتي uicolor ، يتعطل تطبيقي ، مع الخطأ التالي:

"-[__ nscfarray cgcolor]: تم إرسال محدد غير معترف به إلى مثيل 0x7B0AB20 '

Could somebody explain to me what I'm doing wrong. You don't have to be polite.
هل كانت مفيدة؟

المحلول

يجب عليك الاحتفاظ بـ AppColor وإصدارها في طريقة DealLoc الخاصة بك. أنت تخلط على الأرجح مؤشرًا سيئًا على الأرجح

ال دليل برمجة إدارة الذاكرة يمكن أن يكون مرجعا جيدا

نصائح أخرى

يعمل لدي ؛)

#import <UIKit/UIKit.h>

@interface deleteColorAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    UINavigationController *navigationController;
    UIColor *appColor;
}

@property (nonatomic, retain) UIColor *appColor;

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

-(UIColor*)readAppSettings;

@end


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.
    // Add the navigation controller's view to the window and display.

    [self readAppSettings];
    UILabel *label = [[UILabel alloc] init];
    label.textColor = appColor;
    label.text = @"This is a test";
    label.frame = CGRectMake(100, 100, 100, 40);

    [navigationController.view addSubview:label];
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];
    [label release];
    return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application {

    // Save data if appropriate.


}

-(UIColor*)readAppSettings
{
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:@"appSettings.plist"];
    NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];

    float RedComponent = [[plistDictionary objectForKey:@"Red"]floatValue] / 255.0f ;
    float GreenComponent = [[plistDictionary objectForKey:@"Green"]floatValue] / 255.0f ;
    float BlueComponent = [[plistDictionary objectForKey:@"Blue"]floatValue] / 255.0f ;

    appColor = [UIColor colorWithRed:   RedComponent  
                               green:   GreenComponent 
                                blue:   BlueComponent 
                               alpha:   1.0f];
    return [appColor retain];
}



- (void)dealloc {
    [appColor release];
    [window release];
    [navigationController release];
    [super dealloc];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top