How to change image from another class

Discussion in 'iOS Programming' started by Abrexas, Apr 16, 2013.

  1. Abrexas macrumors member

    Joined:
    Jul 29, 2011
    #1
    Hey guys,

    I greatly appreciate all the help I have gotten from this forum! And I am yet again faced with a problem that is probably very easily resolved haha. So I thank you in advance!

    I have two class, ClassA and ClassB.
    ClassA has an image by the name of "backgroundImage"
    I need to set backgroundImage from a button press in ClassB.

    What I have tried.
    In ClassB
    Code:
    @class ClassA;
    @interface ClassB: UIViewController
    
    @property (nonatomic, retain) ClassA *mainView;
    Now upon a button press I am trying to do this.
    Code:
    [ClassA.backgroundImage setImage:[UIImage imageNamed:@"newImage.jpg"]];
    I do not get any errors, but I also don't get any results. Nothing is changed.
    Any suggestions?

    Thanks,
    -Steven French
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Can you explain the difference between ClassA and mainView?
     
  3. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #3
    mainView was just the name I gave to ClassA. At least that's what I thought that line of code was doing haha.
     
  4. dejo, Apr 16, 2013
    Last edited: Apr 16, 2013

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Describe the concept that each of these elements represent. Also describe how they relate to each other.
     
  5. truehybridx macrumors member

    Joined:
    Dec 6, 2010
    #5
    Think really carefully about what you are trying to do, the problem is staring right at you :D
     
  6. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #6
    Okay, here is a better description.

    ClassA is my main class. This viewController has several buttons and a background image. One of these buttons links the user to a "settings" page (which is labeled ClassB). ClassB has a separate viewController linked by a segue. In ClassB I need the user to be able to click a button that changes the backgroundImage in ClassA so that when the user goes back to ClassA, the background is now set to what the user clicked on.
    It's a very simple concept and layout, I am just having trouble setting the image in ClassA from ClassB

    Hope this cleared things up
     
  7. dejo, Apr 18, 2013
    Last edited: Apr 19, 2013

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    That's not really what I was asking. Let me try again:

    Code:
    @property (nonatomic, retain) ClassA *mainView;
    What concept (in terms of Objective-C programming) is ClassA in the above line that defines the property?

    I'll give you the answer I'm looking for: it's the class name and defines the type of object that mainView is (or, better yet, references via a pointer).

    What is mainView?

    It's the name of your property. Through your accessors, other classes can set/get the property (which has an instance variable backing it) and well as being able to reference it internally via self.mainView.

    So, given that, can you explain what ClassA and backgroundImage are, given this line of code?:
    Code:
    [ClassA.backgroundImage setImage:[UIImage imageNamed:@"newImage.jpg"]];
     
  8. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #8

    Hint: The quoted code is wrong. dejo is trying to lead you to figure out what's wrong for yourself, and how to fix it. Teaching a man to fish, and all that.
     
  9. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #9
    Okay I think I understand now. The code I was using was all taken from a search result that I found on stackoverflow.
    I believe the problem may be that by creating a pointer to ClassA, I am only creating a reference, not an actual link to the original ClassA? This would explain why the code does not give an error, but does not do what I expected. That being the case, how do I make a direct link to the ACTUAL ClassA and give ClassB the ability to change backgroundImages?

    I assume it would work the same way that I access variables in my AppDelegate, but the syntax would be slightly different.
     
  10. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #10
    You're close.

    ClassA refers to the Class, not an instance of that class.

    Think of a car factory. The car factory class creates car objects.

    If you want to check the gas gauge on a car, you check the gas gauge on one, specific instance of a car, not the factory. It would not make sense to ask the car factory how much gas a car has. Which car?

    When you create an instance of a ClassA object, that is a discrete thing. If you later create another one, it is a second instance of that class, that has its own instance variables.

    If you want to pass an image to a ClassA object, you pass it to a particular ClassA object, not the whole class (You tell a car to change it's radio station, you don't tell the car factory. My car is likely set to a different radio station than yours.)
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Abrexas, it sounds to me like you still don't have a proper understanding of the fundamentals of Objective-C. I would suggest you step away from the real coding for now and spend some time becoming comfortable with the basics.
     
  12. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #12
    Either the code you posted isn't from your source, or you have created a global UIImage variable in the ClassA definition. If that latter, then of course you won't see a change because the class, assuming some subclass of UIView, doesn't perform displaying. Instances of a UIView, objects, not classes, can display.

    You have defined an object, but you are not setting the background image in it. Also, for it to appear, it will have to be subview of another displayed view.

    You need to learn the difference between a Class and an Object. Probably the view hierarchy too. As dejo suggested, learn the basics before jumping into the deep end.
     
  13. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #13
    My app that I am writing is nearly complete and is a lot more complicated than the question I am asking. I didn't show any of my apps code because the labels would make no sense without understanding the whole thing. I have a question about this one thing, and instead of helping me solve it, most of you just tell me to go read a book... So thanks to the few of you to help me solve this, and less thanks to those of you who inaccurately assumed my level of understanding of objective c.

    As I have mentioned, this method was taken straight from stackoverflow since my original attempt didn't work.
     
  14. Sonnestah macrumors regular

    Joined:
    Mar 2, 2013
    #14
    Funny because the ones asking you to read books and basics are the ones helping you the most.

    If they inaccurately assumed your level of understanding of obj-c, I am more than sure that you would have no problem solving such a basic problem.

    You can always resort to copy-pasting code anyway but then you will never learn. It is all up to you.
     
  15. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #15
    I understand that this is their attempt at helping, but it is a very frustrating one. I am beginning to assume that they don't understand my question exactly for I have done a lot of research and there is no simple or standard solution. I am trying to do this without making a singleton or passing variables through a segue, both of which I have done before. This is something that can be done in java relatively easily, and I doubt the people "answering my question" know the direct answer to my "basic problem".
     
  16. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    That would seem to be the case, for me at least. As such, I don't think I can be of any further assistance. Good luck with this problem and in the future, though!
     
  17. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #17
    Possibly we don't understand what you're trying to do.

    Post the solution to your question in Java so we know exactly what you're trying to do, and we can guide you to the closest equivalent code in Obj-C.
     
  18. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #18
    Your posts indicate a basic misunderstanding of object oriented concepts.

    The code


    Code:
    [ClassA.backgroundImage setImage:[UIImage imageNamed:@"newImage.jpg"]];
    
    Is attempting to send an image to a property of an entire class, not an instance of a class.

    Objective C does not have class variables at all.

    Even in a language like Java that does have class variables, that code does not make sense. What would setting a background image for an entire class DO? Would it tell all instances of the class that they should set a background image? Would it tell the ClassB class factory that new instances of ClassB should use the specified background image?

    Please state clearly what you are trying to do, as well as how your program is structured. If you are fluent in Java, explain your goals in basic Object-oriented software terms rather than language-specific terms.

    The description of the structure of your program will necessarily be iOS and Objective C specific. Are you using XIB files or storyboards? Are you using ARC or manual reference counting?

    My understanding is that you want to pass a background image from one view controller that is an instance of ClassA to another view controller that is an instance of ClassB.

    If you want concrete help, you need to tell us how these view controllers are being created and managed. You say you want to pass the background image on a button press. Does the other view controller (the instance of ClassB) exist at the time of the button press? Are you getting ready to invoke it in a segue? Are you going to create it manually and push it onto a navigation controller or present it modally?
     
  19. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #19
    Dejo, thank you for all of your help in the past.

    As I have mentioned before, the method of doing this that I posted was something that obviously did not work, otherwise I would not have been asking this question. Also, I mentioned that it was not my original idea or code, and that it was a solution I found from a similar question on Stackoverflow.com.
    I am not stating that I know exactly how this is done in java, but that I have done similar things with relative ease.

    I am sorry that you do not understand what I am trying to do, I thought I had made it clear, but I guess not and I will try explaining it again.

    I have two classes (ClassA and ClassB). Each class has its own ViewController.
    ClassA contains an image called backgroundImage and a segue link to ClassB's ViewController.
    ClassB has a selection of possible backgrounds for the user to choose from.
    Upon choosing a background that is displayed in ClassB, I want to set that image to backgroundImage in ClassA.
    What needs to be done here is either ClassB needs access to the backgroundImage variable in ClassA, or ClassB needs to send the desired image name to ClassA so that ClassA can use it to change backgroundImage.

    This seems to be very cut and dry to me. I know this is not a relatively difficult question, but I have done a lot of searching, and have found no convenient solutions.
     
  20. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #20
    Each class has its own ViewController? Sorry, I'm not even sure what that means.
     
  21. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #21

    Ok, so you start with a VC of class ClassA. The ClassA VC invokes a segue to a ClassB VC. In the ClassB VC, the user picks an image. You want to pass that image back to ClassA.

    Here's what you do:

    Define a protocol that defines a backgroundImage property. Have ClassA conform to that protocol. Let's call the protocol BGImageProtocol.

    Have ClassB define a delegate. Make define the delegate as a view controller that conforms to the BGImageProtocol.

    In the ClassA implementation of prepareForSegue, get a pointer to the destination view controller, cast it to type (ClassB *), and set the destination view controller's view controller's property to self.

    In the code for the ClassB view controller, when the user picks an image, set pass the image to your delegate:

    Code:
    self.delegate.backgroundImage = theImageTheUserSelected;
    Then return to the previous ClassA view controller.

    If you don't know how to define a protocol, how to declare that your class conforms to a protocol, or that your delegate conforms to a protocol, do some reading.

    I gotta say that you seem to be to early in your learning of Objective C and iOS to be trying to write full-fledged apps yet. It sounds to me like you are trying to cut-and-paste code to cobble together an app without understanding what the code does. That won't work. That's like trying to build a computer by collecting random parts at a computer store without knowing what pieces fit with the others. If you pick pieces at random, the CPU won't fit in the motherboard, the graphics card will be incompatible, the power supply won't fit inside the case, the case won't be big enough, the hard drive will use the wrong interface (IDE vs SATA vs SCSI), etc, etc.
     
  22. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #22
    Is that what he means by "each class has its own ViewController"? That each class is a subclass of UIViewController? If so, wow. I would never phrase a subclass relationship as "having".
     
  23. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #23
    I think he's really confused. I just ignored that as not meaningful.
     
  24. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #24
    Thank your for that insight. Up until the last part. No disrespect but you have no idea what code I have already done and understand. Sure I may not be fully caught up on terms and specifications, but i have accomplished a lot and only refer to copy-paste as a last resort. I am writing an app for a small community of people, and my method of completing the app has no relevance to my question at hand.

    So thank you for your most recent input, but i have since found a different solution.
     
  25. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #25
    Glad to hear you were finally able to work it out.
     

Share This Page