PDA

View Full Version : How to add a set of object runtime?




Armin1983
Feb 2, 2012, 12:51 AM
Hello guys,

Here is what i want to implement, In the following picture i have a set of objects,
http://forums.macrumors.com/attachment.php?attachmentid=322708&stc=1&d=1328165328

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

http://forums.macrumors.com/attachment.php?attachmentid=322708&stc=1&d=1328165328
http://forums.macrumors.com/attachment.php?attachmentid=322708&stc=1&d=1328165328



gnasher729
Feb 2, 2012, 02:34 AM
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.

Armin1983
Feb 2, 2012, 03:08 AM
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.

Thanks a lot, But I am not perfect at English, Would you mind telling me how to ask such this question?

KnightWRX
Feb 2, 2012, 04:18 AM
Thanks a lot, But I am not perfect at English, Would you mind telling me how to ask such this question?

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 ?

Armin1983
Feb 2, 2012, 04:39 AM
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 ?
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.

robbieduncan
Feb 2, 2012, 05:03 AM
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.

amorya
Feb 2, 2012, 05:25 AM
Is your current UI done in IB or in code? If it's IB then prepare for a bit of a culture shock :P

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.

gnasher729
Feb 2, 2012, 06:02 AM
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.

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:

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.

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.

KnightWRX
Feb 2, 2012, 06:37 AM
Thank you for your guidance, It is related to english as well since i can't use the right terms,anyway

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.

Armin1983
Feb 2, 2012, 09:23 AM
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.

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. :)