How to add a set of object runtime?

Discussion in 'Mac Programming' started by Armin1983, Feb 1, 2012.

  1. macrumors newbie

    Joined:
    Jan 29, 2012
    #1
    Hello guys,

    Here is what i want to implement, In the following picture i have a set of objects,
    [​IMG]

    How can i multiply this set runtime below it, Just when i press another button?

    so for example if i press add more, its will be look like this

    [​IMG]
    [​IMG]
     

    Attached Files:

  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Before we go any further, have a look at the thread title: "How to add a set of object runtime?" What is that supposed to mean? You are trying to get help hear; can't you even be bothered to pick a thread title that expresses what you want in meaningful English?

    The "objects" that you are talking about are called "views".

    Create the objects that you want to add. They most likely are instances of some subclass of NSView, so the right [xxx alloc] and initWithFrame will likely do it.

    Figure out where they go in your view and use setFrame.

    Add them to the parent view using addSubview.
     
  3. thread starter macrumors newbie

    Joined:
    Jan 29, 2012
    #3
    Thanks a lot, But I am not perfect at English, Would you mind telling me how to ask such this question?
     
  4. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #4
    Has nothing to do with english, everything to do with the programming jargon.

    "How to add a set of objects at runtime", to us means you're trying to dynamically create objects/classes during runtime. We come into the thread, expecting to talk about pointer allocation, arrays, dictionaries and other stuff.

    Then we see you're talking about dynamically generating UI elements.

    So a better thread title would be :

    "How do I add UI elements at runtime"

    Now. How did you create your Path label/text input/button ? Is it a single view ? Is it part of some view that is much bigger ? Is it loaded from a Xib or programmatically generated ?
     
  5. thread starter macrumors newbie

    Joined:
    Jan 29, 2012
    #5
    Thank you for your guidance, It is related to english as well since i can't use the right terms,anyway

    label and textbox and button are added through interface builder. but i want to multiply these veiws when i need.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Is your current UI done in IB or in code? If it's IB then prepare for a bit of a culture shock :p

    If this was me I'd be use a NSTableView with each set of controls that you want to duplicate as a new row. Getting this to work will require a decent knowledge of generating UI in code and how to customise the view shown for a row/column pair in a table view.
     
  7. macrumors 6502

    Joined:
    Jun 17, 2007
    #7
    You can get something like this working in IB-land, using NSViewController and NSCollectionView, along with bindings to an array of objects that represent the contents of each slice.
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    Start by writing a method that gets called at the time where you want to add the new views. That method will likely be an IBAction. Inside the method, the only code you should have at this time is a single statement:

    Code:
    NSLog (@"I want to add some views");
    and get that to work.

    Next, add code to the method that finds out in which view you want to add the new views. Then print some info about the view and its subviews.

    Code:
    NSView* viewToAddTo = ...;
    NSLog (@"viewToAddTo = %@ with frame %@", viewToAddTo, NSStringFromRect (viewToAddTo.frame));
    for (NSView* subview in viewToAddTo.subviews)
        NSLog (@"subview = %@ with frame %@", subview, NSStringFromRect (subview.frame));
    (How do you find the view? Most likely its the superview of the button that you clicked, and that button would be the sender in the IBAction method.)

    Come back when that is working properly.
     
  9. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #9
    Everyone else chimed in to help you create your objects, so I'll just address this : Again, has nothing to do with english. Even if your english is poor, you're writing code and should know the jargon. Objects instances/classes are different from UI elements, Views, SubViews, no matter what your primary spoken/written language is.

    Mine is french, un objet is an object. Une vue is a view. I would never translate une vue to an object. Des elements de l'interface are UI elements.

    These same translations exist for your own primary language I'm sure. You should definately understand the difference between objects (when talking about OOP) and views/subviews/ui elements.

    If not, learn the jargon right. It will help in 2 ways that are very important to your programming career :

    1- Help you search the documentation/other references for how to do things. If you go to Google and you typed "add objects at runtime", you'd probably get a bunch of false positive hits for memory allocation, using dictionaries to build "objects" with different properties at runtime and other OOP programming tips and tricks instead of your UI documentation. This is much faster than asking for help everytime.

    2- Helps you get help from forum goers. If you fail at #1 for any reason, getting help from other humans requires that you can interact with them. Language barrier ? In programming, it shouldn't matter. We all speak a common language here : Objective-C, Java, C, C++, Python, Ruby, whatever. If you know the jargon, you can get understood by the other posters who will then help you.
     
  10. thread starter macrumors newbie

    Joined:
    Jan 29, 2012
    #10
    Merci, for time to help me, Yes you are right, Now i can say you are right in this regard. I will spend more time next time to use right terms for asking questions. :)
     

Share This Page