|
|
#1 |
|
Creating Object From String
I am new to Obj-C and as my first project I decided to make a Mac version of a program I had made before in C#
Basically the program is a counter or score keeper or whatever you want to call it! There are 9 + and - buttons each pair connected to a label So instead of having 18 Methods I made 2 which work dynamically Now I am trying to do the same thing in Obj-C Here is my code Code:
NSString *which = "L1"; //Label 1 NSTextField *label = (NSTextField *)NSClassFromString(which)]); Thank You! |
|
|
|
0
|
|
|
#2 |
|
The proper format for an NSString literal is
Code:
@"string here" Also, you don't ever want to call NSClassFromString usually. What are you trying to do here exactly? C# is a very different language from Objective-C. You may want to check up on the Objective-C videos on iTunes U from Stanford. They're quite good and should get you on the right track.
__________________
Con + Cat Last edited by ConCat; Feb 11, 2013 at 11:09 PM. |
|
|
|
0
|
|
|
#3 |
|
I think you're a bit confused here. You seem to be trying to use NSClassFromString to generate a “generic” object, then casting that object to an NSTextField. NSClassFromString returns the class that is named in the string, so you would pass it something like "NSString" or "NSView" (and then call alloc on the returned class). But as ConCat mentioned, you probably don't want to be using it, because if you know what the class type is at compile time then you should just use that.
Also, are you aware that NSTextField is a UI element? It's not the same thing as a string. Usually you would just add an NSTextField in Interface Builder (now part of Xcode), but you can generate one programmatically and add it to a view that you have an outlet for if you like. Just remember that an NSTextField contains an NSString (which holds the contents of the field), and this NSString must be set once the NSTextField is instantiated by using the appropriate setter method (in this case, setStringValue: ). |
|
|
|
0
|
|
|
#4 |
|
I dont know if I made it clear what I am trying to do, I was probably vague and my code made it worse.
Picture a window like this 0 0 0 0 0 0 0 0 0 +- +- +- +- +- +- +- +- Now I dont want to have a method for each + and for each - So instead I gave the +'s and -'s names Like A1 A2 M1 M2 and I also gave the Labels names L1 L2 So I have two methods, 1 for Add, and one for Subtract I use the sender object to get the number and then I put a L in front of it to know which label. But I cant seem to figure out how to do this. Here is my C# code with comments added Code:
private void Add(object sender, EventArgs e)
{
PictureBox pic = (PictureBox)sender; //Which add was clicked
int number = int.Parse(pic.Name.Substring(3));//take the number
TextBox tb = (TextBox)this.Controls["textBox" + number];//Make a object with the number
//Rest isn't important
int num = g[number];
num++;
g[number] = num;
String output = num.ToString();
if (tb != null)
tb.Text = output;
}
|
|
|
|
0
|
|
|
#5 | |
|
Quote:
But if you cannot write an Objective-C/Cocoa program on its own, translating C# to Objective-C isn't going to work. |
||
|
|
0
|
|
|
#6 | |
|
Quote:
Code:
- (IBAction)add:(id)sender
{
// Code to to manipulate the text boxes here.
}
If you want to programatically add or remove arrays of controls (say you want to dynamically add or remove players from your scoreboard), you might be interested in looking up NSMatrix and the Matrix Programming Guide.
__________________
15'' Early 2011 MBP | iPhone 5 | iPad (2012) | MacOS X 10.8.3 |
||
|
|
1
|
|
|
#7 |
|
I think I am just going to use a array to store the score and then use a refresh method to update the labels. It's worked so far.
|
|
|
|
0
|
|
|
#8 |
|
What is your goal here? If you want this app to use and aren't so concerned with learning things, it seems to me this is an extraordinarily easy app to make without writing a single line of code.
Drag and drop in your 9 fields. Drag and drop in 9 steppers. Use cocoa bindings to bind each field to each stepper. All of that can be done in your interface file (XIB) alone. |
|
|
|
0
|
|
|
#9 |
|
Heck, it's even easier than that. Just drag out an NSTextField and an NSStepper. Give the NSTextField a title of “0”. Set the appropriate parameters for your NSStepper (minimum and maximum values). Ctrl-drag from the stepper to the text field, and assign the takeIntValueFrom: action. Select both items and duplicate them as many times as you want. Done.
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 01:43 PM.








Linear Mode
