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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1607498
  • 79
  • 1
  • 8
  • Please specify what language it is . – coredump Sep 20 '12 at 09:25
  • oh - sorry :) it´s objective c in xcode editor – user1607498 Sep 20 '12 at 09: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 '12 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 – user1607498 Sep 23 '12 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. – Jack Lawrence Sep 23 '12 at 15:34
  • Im pretty sure I don´t too, but I am trying to learn though. What would be the right way to pass more than one object between view then? When searching around it seems that almost everyone has an idea about that. I need to be able to pass two strings thats it. and I can see that I will not be able to extend a singleton to keep more than one value - I have tried that quite a few times now with different singleton code - none seems to be able to pass more than one. – user1607498 Sep 23 '12 at 16:39

2 Answers2

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
Community
  • 1
  • 1
uspython
  • 483
  • 5
  • 18
  • 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 '12 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 :) – user1607498 Sep 22 '12 at 09: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 – user1607498 Sep 28 '12 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];
NeverBe
  • 5,213
  • 2
  • 25
  • 39