Creating IBOutlets in loop interface - objective-c

I'm sure there's an easy way to do this:
for (i=0; i<10; i++) {
IBOutlet UIButton *button+i;
}
instead of this:
IBOutlet UIButton *button0;
IBOutlet UIButton *button1;
IBOutlet UIButton *button2;
IBOutlet UIButton *button3;
IBOutlet UIButton *button4;
IBOutlet UIButton *button5;
IBOutlet UIButton *button6;
IBOutlet UIButton *button7;
IBOutlet UIButton *button8;
IBOutlet UIButton *button9;
but my objective-c is not so good. Can someone help?

This is not possible. IBOutlet is a keyword that is interpreted by Interface Builder.
It only makes sense in the .h file, never in any loop.

Related

Count - variable as part of object name [duplicate]

This question already has answers here:
Objective C Equivalent of PHP's "Variable Variables" [duplicate]
(2 answers)
Closed 8 years ago.
How I can integrate a count var (i = 1,2,3,4,...) into a object-name?
My code doesn't work.
for (int i=1; (i<=4); i++) {
self.cmdFertigOutlet(i).layer.cornerRadius = self.cmdAbbrechenOutlet(i).layer.cornerRadius = self.lblYear(i).layer.cornerRadius = self.lblMonth(i).layer.cornerRadius = self.lblDay(i).layer.cornerRadius = self.lblHour(i).layer.cornerRadius = self.lblMinute(i).layer.cornerRadius = self.lblSecond(i).layer.cornerRadius = self.txtBeschreibung(i).layer.cornerRadius = 5;
}
.h file
#property (weak, nonatomic) IBOutlet UIButton *cmdAbbrechenOutlet1;
#property (weak, nonatomic) IBOutlet UIButton *cmdFertigOutlet1;
#property (weak, nonatomic) IBOutlet UITextField *txtBeschreibung1;
#property (weak, nonatomic) IBOutlet UILabel *lblYear1;
#property (weak, nonatomic) IBOutlet UILabel *lblMonth1;
#property (weak, nonatomic) IBOutlet UILabel *lblDay1;
#property (weak, nonatomic) IBOutlet UILabel *lblHour1;
#property (weak, nonatomic) IBOutlet UILabel *lblMinute1;
#property (weak, nonatomic) IBOutlet UILabel *lblSecond1;
#property (weak, nonatomic) IBOutlet UIButton *cmdAbbrechenOutlet2;
#property (weak, nonatomic) IBOutlet UIButton *cmdFertigOutlet2;
#property (weak, nonatomic) IBOutlet UITextField *txtBeschreibung2;
#property (weak, nonatomic) IBOutlet UILabel *lblYear2;
#property (weak, nonatomic) IBOutlet UILabel *lblMonth2;
#property (weak, nonatomic) IBOutlet UILabel *lblDay2;
#property (weak, nonatomic) IBOutlet UILabel *lblHour2;
#property (weak, nonatomic) IBOutlet UILabel *lblMinute2;
#property (weak, nonatomic) IBOutlet UILabel *lblSecond2;
and so on... 3,4
.m file instead of use this (works), i will use the for-statement. i don't understand, how to put my object-names into an array and then use it with the for-statement.
self.cmdFertigOutlet1.layer.cornerRadius =
self.cmdAbbrechenOutlet1.layer.cornerRadius =
self.lblYear1.layer.cornerRadius =
self.lblMonth1.layer.cornerRadius =
self.lblDay1.layer.cornerRadius =
self.lblHour1.layer.cornerRadius =
self.lblMinute1.layer.cornerRadius =
self.lblSecond1.layer.cornerRadius =
self.txtBeschreibung1.layer.cornerRadius = 5;
self.cmdFertigOutlet1.layer.masksToBounds =
self.cmdAbbrechenOutlet1.layer.masksToBounds =
self.lblYear1.layer.masksToBounds =
self.lblMonth1.layer.masksToBounds =
self.lblDay1.layer.masksToBounds =
self.lblHour1.layer.masksToBounds =
self.lblMinute1.layer.masksToBounds =
self.lblSecond1.layer.masksToBounds =
self.txtBeschreibung1.layer.masksToBounds = YES;
self.cmdFertigOutlet2.layer.cornerRadius =
self.cmdAbbrechenOutlet2.layer.cornerRadius =
self.lblYear2.layer.cornerRadius =
self.lblMonth2.layer.cornerRadius =
self.lblDay2.layer.cornerRadius =
self.lblHour2.layer.cornerRadius =
self.lblMinute2.layer.cornerRadius =
self.lblSecond2.layer.cornerRadius =
self.txtBeschreibung2.layer.cornerRadius = 5;
self.cmdFertigOutlet2.layer.masksToBounds =
self.cmdAbbrechenOutlet2.layer.masksToBounds =
self.lblYear2.layer.masksToBounds =
self.lblMonth2.layer.masksToBounds =
self.lblDay2.layer.masksToBounds =
self.lblHour2.layer.masksToBounds =
self.lblMinute2.layer.masksToBounds =
self.lblSecond2.layer.masksToBounds =
self.txtBeschreibung2.layer.masksToBounds = YES;
The object names are like:
cmdFertigOutlet1, cmdFertigOutlet2, cmdFertigOutlet3, and so on.
cmdAbbrechenOutlet1, cmdAbbrechenOutlet2, cmdAbbrechenOutlet3, and so
on
You can't - the variables' names are compile-time properties and don't exist at runtime.
Instead, use arrays with four elements:
for (int i = 0; i < 4; i++) {
self.cmdFertigOutlet[i].layer.cornerRadius = ...
}
I'm going to give you a simple example using NSTextField objects (to keep it simple). I think you'll understand how to carry this forward in your application.
From your syntax, I also assume you're programming in Swift. To demonstrate a technique:
In my AppDelegate class I declare IBOutlets to 4 text boxes and connect them in IB:
#IBOutlet var testTextBox0:NSTextField!
#IBOutlet var testTextBox1:NSTextField!
#IBOutlet var testTextBox2:NSTextField!
#IBOutlet var testTextBox3:NSTextField!
I also declare a mutable array:
var testTextBoxes:NSMutableArray = NSMutableArray()
Then in the applicationDidFinishLaunching function I say:
testTextBoxes.addObject(testTextBox0)
testTextBoxes.addObject(testTextBox1)
testTextBoxes.addObject(testTextBox2)
testTextBoxes.addObject(testTextBox3)
var localTextField:NSTextField = testTextBox0
for var i:Int = 0; i < 4; i++
{
localTextField = testTextBoxes.objectAtIndex(i) as NSTextField
localTextField.stringValue = "text field \(i)"
}
and all the text box string values get set properly.
This illustrates a technique of loading your objects into an array (or arrays for several different object types) and accessing them sequentially in a for loop.
Here's another example in Objective C using button objects this time:
transformButtons = [[NSMutableArray alloc] initWithCapacity:10]; // transformButtons is an instance variable
[transformButtons addObject:button0];
[transformButtons addObject:button1];
[transformButtons addObject:button2];
[transformButtons addObject:button3];
[transformButtons addObject:button4];
[transformButtons addObject:button5];
[transformButtons addObject:button6];
[transformButtons addObject:button7];
[transformButtons addObject:button8];
[transformButtons addObject:button9];
Then later in the code:
NSButton *localButton;
for(int i = 0; i < 10; i++)
{
localButton = [transformButtons objectAtIndex:i];
[localButton setEnabled:YES];
[localButton setHidden:NO];
[localButton setTitle:buttonNames[i]];
}
Of course in the .h file this stuff was declared as:
NSMutableArray *transformButtons;
IBOutlet NSButton *button0;
IBOutlet NSButton *button1;
IBOutlet NSButton *button2;
IBOutlet NSButton *button3;
IBOutlet NSButton *button4;
IBOutlet NSButton *button5;
IBOutlet NSButton *button6;
IBOutlet NSButton *button7;
IBOutlet NSButton *button8;
IBOutlet NSButton *button9;
Hope this helps.
EDIT2:
In the .h file, if you want to access these variables from another class, why not:
#public
IBOutlet UIButton *cmdAbbrechenOutlet1;
IBOutlet UIButton *cmdFertigOutlet1;
IBOutlet UITextField *txtBeschreibung1;
IBOutlet UILabel *lblYear1;
IBOutlet UILabel *lblMonth1;
IBOutlet UILabel *lblDay1;
IBOutlet UILabel *lblHour1;
IBOutlet UILabel *lblMinute1;
IBOutlet UILabel *lblSecond1;
IBOutlet UIButton *cmdAbbrechenOutlet2;
IBOutlet UIButton *cmdFertigOutlet2;
IBOutlet UITextField *txtBeschreibung2;
IBOutlet UILabel *lblYear2;
IBOutlet UILabel *lblMonth2;
IBOutlet UILabel *lblDay2;
IBOutlet UILabel *lblHour2;
IBOutlet UILabel *lblMinute2;
IBOutlet UILabel *lblSecond2;
// And so on.....
#private
// Your private instance variables
In your .m file cmdAbbrechenOutlet1 could simply be accessed as cmdAbbrechenOutlet1. In another class where yourClass is visible, it could be accessed as yourClass->cmdAbbrechenOutlet1.

UIButton to reset UIView

I have 2 UIViews. One is the main menu the second one is the actual game. When it's game over, a Retry button appear, i want the button to reset the UIView so the game can start over. Right now i have the "Retry" button connected to the main menu but i don't like the fact that you gotta go by the main menu to retry.
I tried adding this line of code"
[GameViewController.view setNeedsDisplay]
In my "GameOver" method but i had an error pointed to the .view and it said "property view not found on object of type GameViewController".
EDIT
GameViewController.h
int BirdFlight;
int RandomTopTunnelPostition;
int RandomBottomTunnelPosition;
int ScoreNumber;
NSInteger HighScoreNumber;
#interface GameViewController : UIViewController
{
IBOutlet UIImageView *Bird;
IBOutlet UIButton *StartGame;
IBOutlet UIImageView *TunnelTop;
IBOutlet UIImageView *TunnelBottom;
IBOutlet UIImageView *Top;
IBOutlet UIImageView *Bottom;
IBOutlet UIButton *Exit;
IBOutlet UILabel *ScoreLabel;
NSTimer *BirdMovement;
NSTimer *TunnelMovement;
SystemSoundID PlaySoundID;
SystemSoundID PlaySoundID2;
SystemSoundID PlaySoundID3;
}
-(IBAction)StartGame:(id)sender;
-(void)BirdMoving;
-(void)TunnelMoving;
-(void)PlaceTunnels;
-(void)Score;
-(void)GameOver;
#end

Access to button from Storyboard

Stupid and simple question, but I have to ask.
I dragged on Storyboard a button.
How to get access to it?
With drag "Ctrl" not offer.
How do I do it programmatically change setVisible(Yes/No)?
You need an IBOutlet to your UIButton
in your view controller h file
IBOutlet UIButton *mybutton;
#property (nonatomic, retain) UIButton *mybutton;
in your m file
#synthesize mybutton;
and then you should be able to do:
[mybutton setAlpha:0];

UITextField bind

I can't figure out why that code is not working for me.
ViewController.h
...
#property (nonatomic, copy) UITextField *textField;
...
ViewController.m
-(void)viewDidLoad
{
[self.textField addTarget:self
action:#selector(textIsChanged:)
forControlEvents:UIControlEventEditingChanged];
}
-(void)textIsChanged:(id)sender;
{
NSLog(#"Changed");
}
When I type something in the textField textIsChanged method is never invoked.
You should declare textField as an IBOutlet like this:
#property (nonatomic, retain) IBOutlet UITextField *textField;
or, if you are using ARC (Automatic Reference Counting):
#property (nonatomic, strong) IBOutlet UITextField *textField;
and bind it from the xib file in interface builder.

UIButton removeFromSuperview

I went through guide from Apple "Your first iOS app"
and now I have a button, which is not declared in ViewController:
#interface HelloWorldViewController : UIViewController <UITextFieldDelegate>
- (IBAction)changeGreeting:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (copy, nonatomic) NSString *userName;
#end
Now i can remove label (and textField), using [label removeFromSuperview]; but i dont understand how to do it with button. Can someone help?
You should add an IBOutlet to the button as you did for the textfield and the label:
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UIButton *button; // Don't forget to link to this from Interface Builder
// ...
Then you can remove the button using:
[button removeFromSuperview];
Also note that the tutorial you linked to says:
The sender parameter in the action method refers to the object that is sending the action message (in this tutorial, the sender is the button).
So if you want to remove the button when it is tapped (inside changeGreeting:), then you don't need the IBOutlet because you already have a reference to the button in the sender parameter:
- (IBAction)changeGreeting:(id)sender
{
UIButton *button = (UIButton *)sender;
// ...
[button removeFromSuperview];
// ...
}
You need to declare the button in the controller like you did as an IBAction and this time declare this as an Outlet(IBoutlet).. this way you will get its reference in code..
Alternatively .. you can set a tag to the button in Interface Builder
..
and then retrieve in code using viewWithTag: method