0

I have use a singleton class that contained just one passed value. I then tried to add another one.

#import <Foundation/Foundation.h>

@interface GlobalValueContainer : NSObject {


    NSString *passedText;
    NSString *myPassedPictureName;
}


@property (nonatomic, strong) NSString* passedText;
@property (nonatomic, strong) NSString* myPassedPictureName;


+ (GlobalValueContainer *) sharedStore;

@end


#import "GlobalValueContainer.h"

@implementation GlobalValueContainer;

@synthesize passedText;
@synthesize myPassedPictureName;

static GlobalValueContainer *sharedStore = nil;


+ (GlobalValueContainer *) sharedStore {
    @synchronized(self){
        if (sharedStore == nil){
            sharedStore = [[self alloc] init];
        }
    }

    return sharedStore;
}


@end

From the first view I then try to set the myPassedPictureName

-(IBAction)setPicture:(id)sender{

    myPicture = @"Hus";
    GlobalValueContainer* localContainer = [GlobalValueContainer sharedStore];
    localContainer.myPassedPictureName = myPicture;


}

and on the second view I want to set an imageview with that name (+png that is)

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Store* myStore = [Store sharedStore];


    GlobalValueContainer* localContainer = [GlobalValueContainer sharedStore];
    myPassedPictureName = localContainer.myPassedPictureName;
    myPicture.image = [UIImage imageNamed:myPassedPictureName];
    whatFile.text = myPassedPictureName;

   //object.imageView.image = [UIImage imageNamed:@"downloadedimage.png"];

}

the picture doesnt show. I have also tried to add a UIlabel and set the string that should have been passed. But it also turns out blank.

When I pass text using the "passedText" it works fine. When I added the second NSString, nothing happens?.

First things first. Can anyone see what´s wrong (still learning obj c here :) and, is it the correct way I try to manipulate an UIImageView. I want to use the myPassedPictureName to set a picture on a number of UIViews depending on the button being pressed.

Looking forward to your input.

6
  • Please specify what language it is .
    – coredump
    Sep 20, 2012 at 9:25
  • oh - sorry :) it´s objective c in xcode editor Sep 20, 2012 at 9:32
  • I don't see anything obviously wrong with your code. Have you tried inspecting the variable's value in that method? Have you tried manually replacing it with the image name to confirm you aren't getting the name wrong?
    – Chuck
    Sep 21, 2012 at 22:02
  • I think my singleton is "messed" it can´t handle two variables. *passedText works fine - us use that "all over" myPassedPictureName remount null even after I try to set it in the IBAction Sep 23, 2012 at 10:44
  • I don't think you totally understand what Objective-C properties are for, the delegate protocol design pattern, and what the proper use-case of a singleton is. Singletons are the easy way out to store state. It's usually much less brittle and makes more sense to pass your information along from one object to another directly. Sep 23, 2012 at 15:34

2 Answers 2

0

I'm quite sure using singleton for passing value like this is not a good idea.Singleton is not designed for passing value,but for doing something.So you can not use Property like
sharedManager.passValue
Here has some good discussion about singleton.
When should you use the singleton pattern instead of a static class?
and
What should my Objective-C singleton look like?
So I suggest write it like this:

#import <Foundation/Foundation.h>

@interface GlobalValueContainer : NSObject {

}

+ (id) sharedManager;
-(NSString*)myText;
-(NSString*)myPictureName;

@end


#import "GlobalValueContainer.h"

@implementation GlobalValueContainer;


static GlobalValueContainer *sharedManager = nil;


+ (id) sharedManager {
    @synchronized(self){
        if (sharedManager == nil){
            sharedManager = [[self alloc] init];
        }
    }

    return sharedManager;
}

-(NSString*)myText
{
    return @"your text";
}
-(NSString*)myPictureName
{ 
    return @"yourPictureName.png";
}  
@end
3
  • While I wouldn't design it this way, I don't see why you think this is something that you "can't do." It isn't technically infeasible; just awkward. Am I missing something?
    – Chuck
    Sep 21, 2012 at 22:00
  • Well, im veeery new at this. So im working my way around to find the correct way of doing "things" I need to be able to pass up to three nsstrings between the views, and I have not been able to find a "better" way to do it than this way. I am pretty open to suggestions though :) Sep 22, 2012 at 9:39
  • It looks like the singleton worked after all. I scratched the view and recreated it - it all works. I used the same code as before. Must be an xcode bug or something Sep 28, 2012 at 18:39
0

If variable "myPassedPictureName" is URL to file then you cannot use this method for it:

 [UIImage imageNamed:myPassedPictureName];

you should use

NSData *data = [[NSData alloc] initWithContentsOfURL:myPassedPictureName];
UIImage *image = [[UIImage alloc] initWithData:data];
1
  • it´s not a URL. I hardcode a filename to the IBAction the png file is a resource in my project Sep 23, 2012 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.