XCode/Objective-C, load buttons (and other UI elements) at runtime - objective-c

Is it possibile to load buttons at runtime without using .xib files and connect the buttons to class properties anyway (without drag&drop)?

With the "load without .xib" statement do you mean "instantiate in the code", right? in such case the answer is "yes, of course":
// interface
#property (retain) UIButton *myButtonProperty;
// implementation
UIButton *tmpButton = [UIButton buttonWithType:...];
...
self.myButtonProperty = tmpButton;

Yes, it is. You can create button object and add it to a view. More details can be found here.

Related

How I can get the value of my UITextField?

how I can get the value of my UITextField ? When I declare my UITextField in the Storyboard, I know but like this, I don't know.
(sorry for my English, I'm French)
Thank you in advance for your answer.
//Ajout d'un Text Field
CGRect rectTF = CGRectMake(10,70,100,20); // Définition d'un rectangle
UITextField *articleSaisi = [[UITextField alloc] initWithFrame:rectTF];
articleSaisi.borderStyle = UITextBorderStyleLine;
articleSaisi.placeholder = #"Article";
[self.view addSubview: articleSaisi];
I am not quite sure whether you want to access the value or you want to associate your UI element to your code:
Try this if you are saying that you want to access the text value.
atricleSaisi.text
Try to control drag the UI element to either your corresponding .h or .m file so that it can create IBOutlet for you if you are saying that you want to connect your UI element to your code.
Highly recommend you go check the documentation.
if you want to know when the user pressed return, as per your comment, then you should create a UITextViewDelegate for your text view and define its textFieldShouldReturn method:
Discussion
The text field calls this method whenever the user taps the return button. You can use this method to implement any custom behavior when the button is tapped.
Also, give a look at textFieldDidEndEditing, which is called whenever there is a focus change and you should define to correctly handle user input.
Old answer:
If I do not understand you incorrectly, you want to create a UITextField programmatically (i.e., not through a Storyboard).
In this case, you should put you initialisation code inside the viewDidLoad method of your view controller and make sure that you define a property for the text field instead of a local variable:
#interface MyViewController : UIViewController
....
#property (nonatomic, strong) UITextField* articleSaisi;
...
#end
- (void)viewDidLoad {
CGRect rectTF = CGRectMake(10,70,100,20); // Définition d'un rectangle
self.articleSaisi = [[UITextField alloc] initWithFrame:rectTF];
self.articleSaisi.borderStyle = UITextBorderStyleLine;
self.articleSaisi.placeholder = #"Article";
[self.view addSubview:self.articleSaisi];
}
If you do so, you can access the text field value from any other method in the view controller through its text property:
self.articleSaisi.text

Set textField.text from other class of my app

I am trying to solve my problem with searching in Developer Library but probably i dont know for what should i search.
I have HeadViewController.h where i set property of my IBOutlet textField.
In that view i have another view (containerView) with ContainerViewController class.
My problem is that i cant set textField text in my HeadViewController even if i try to set property of my HeadViewController class in my ContainerViewController.h
Here is what am i trying to:
ContainerViewController.h
#class HeadViewController;
#property HeadViewController *hvc;
ContainerViewController.m
#import "HeadViewController.h"
// in my IBAction method
self.hvc.textField.text = #"text from container view";
I also trying to use [textField setText:#""] and [textField inserText:#""] but with same result. If anyone know where is my problem or for what i have to search in Developer Library i will appreciate that.
You need to set the value of your property, hvc. hvc is a pointer, but just declaring it doesn't make it point to anything, it just says what class of object it should point to. Since ContainerViewController is embedded in a container in HeadViewController's view, that makes HeadViewController the parent. So you can reference HeadViewController from ContainerViewController by using self.parentViewController.
// in my IBAction method
self.hvc = (HeadViewController *)self.parentViewController;
self.hvc.textField.text = #"text from container view";

Two States -> Two Actions -> One UIButton for iOS 7

I want to accomplish something that It's even hard for me to explain to you, but I'm going yo try to do.
I have one UIButton (Button 1) and want it do to two different actions depending on the context or state of this particular button, but the context or state is controlled by another UIButton (Button 2).
I hope this image can explain what I'm looking for:
Now... i'm not looking for the exact code I need to implement, but an idea of how to accomplish this, maybe some hint about some methods or classes to use.
Note: I already think about to change the UIButton tag to control the action, but I can't change the tag because I need it as it is to control the behavior of actions. Also, (Button1) must return to it's original state once the second action it's used.
From what you've described, I suggest you should have two IBActions and set a property of UIButton 1. Your .h header file will look something like -
-(IBAction)button1Pressed:(id)sender;
-(IBAction)button2Pressed:(id)sender;
#property (strong, nonatomic) IBOutlet UIButton* button1;
Where the button1 property is hooked up to your button one via ctrl-drag in the interface builder.
Then within your .m implementation file, the following set-up will enable you to do what you're looking for.
-(IBAction)button2Pressed:(id)sender{
if (self.button1.selected){
self.button1.selected = NO;
}else
self.button1.selected = YES;
}
So when you press button2, this will change the state of button1 by making it 'selected' or not, which means button1 will do what ever you need, where the state is controlled by button2.
-(IBAction)button1Pressed:(id)sender{
if (self.button1.selected){
//Place your code here for button 1 to do something in this state
} else
//Place your code here for button 1 to do something in this UN-selected state
}
I hope this helps with what you're trying to do.
Thanks, Jim.
You can use the UIButton's UIControlState, with if statements.
(i.e
buttton.selected....
buttton.enabled.....
buttton.highlighted....
)
if I understand correctly?

IBOutletCollection NSMutable Array order in IB

I have linked a set of buttons to and IBOutletCollection NSMutableArray;
I want to be able to make each button play a specific sound;
However after linking them I realised they were not added in any particular order.
I think the interface builder doesn't take into account the order of the added buttons.
Is there any way I could possibly access the interface button id through code and cast it as an integer so I could programatically assign the right sound to the right button?
Don't think is necessary but here is my code:
for(int i=0;i<48;i++){
UIButton *Pad=[UIButton alloc];
[Pads addobject:Pad];
[Pad release];
[[Pads objectAtindex:i] addTarget:self action:#selector(play:) forControlEvents:UIControlEventTouchUpInside];
((UIButton*)[Pads objectAtIndex:i]).tag=i;
}
To be more specific. In the interface builder in the atributes inspector each button has a label and an id. Would it be possible to reorder my array programatically using that id(or label for that matter)?
FIXE by sorting by TITLE [button currentTitle]
Simply set the tag property of each IBOutlet to i when adding it to Pads.
IBOutletCollection do not support ordering. Here's a similar question with an answer which orders collection contents by their position:
IBOutletCollection set ordering in Interface Builder

Getting objects like button from view. iOS

I created a button in my cover.h file for my first view
#import <UIKit/UIKit.h>
#interface cover : UIViewController
{
IBOutlet UIButton *Enter;
}
#property (nonatomic, retain) UIButton *Enter;
-(IBAction)buttonpressed:(id)sender;
#end
and connected it with the actual button in the inteface builder by choosing File's Owner in the tiny box that gives you the choice of File's Owner, First Responder, and View
Then I went to cover.m file and added the following code
-(IBAction)buttonpressed:(id)sender
{
[[NSBundle mainBundle] loadNibNamed:#"nextView" owner:self options:nil];
NSLog(#"pressed");
}
SO when I go to the nextView.xib and modify nextView.m and nextView.h and access its buttons and do the same thing I did for cover.xib cover.m and cover.h , it doesn't work properly.
What happens is that when I click the enter button in the cover view it shuts down the app. This does not happen until I connected the button to function and outlet (Meaning when it was just switching views and the second view wasn't doing anything it would work)
Thank you for any help you can give. Sorry if I haven't given enough information, kinda new at this, but as find out more info I should have had, I will add it.
Thank you
Edit 1 :
I did not notice anywhere where it said there was an error or anything like that. It built correctly
It's convention to capitalize class names. I'd Suggest CoverViewController. This makes it more obvious, when reading your code, what we're looking at.
What is your intent here? To show nib after nib of content?
Is the "files owner" of every nib a CoverViewController?
What you have actually done is (catastrophically) reloaded the views for the existing controller. This will not end well.
What you want to do is create another instance of the same class:
-(IBAction)buttonpressed:(id)sender {
CoverViewController nextController = [[CoverViewController alloc] initWithNibName:#"nextView"];
[navigationController pushViewController:nextController animated:NO];
}
If you're not using a nav controller, you probably want to be.
Read up on the View Controller Programming Guide.