iPhone Create UIButton in IB for custom UIView - iphone

I have a custom UIView (created without InterfaceBuilder) in my app. And I want to insert an UIButton into it, but I don't want to create the button prorammatically.
So I want to create UIButton in Interface Builder and then put it into my view.
My first steps were:
1) Create XIB from "Empty" template
2) Drag RoundRect button from Objects Library onto this XIB
3) Save XIB as "MyButton.xib"
were these steps correct? and what's next?

IMHO connect the View and the UIButton from your XIB-File to your custom UIView-Class.
After that creates the click-method for this button and have fun :)

Related

iOS , Same UIButton with same selector on Multiple View Controller

I'm a newbie iOS developer, and I'm currently facing a problem.
I had to add the same UIButton manually on multiple View Controllers.
Now the code for creating it is repeated in every View Controller with that button.
Now I have to code the selector which is called by the button (An action sheet will be called) but I don't know if repeating the code in every controller is the best thing to do.
Is there a way to write only once the code for the action and call it from every view controller?
Thank you
You can create a UIButton Subclass and then add it to each view. The button behavior and properties will be stored in the class and that way you won't have to repeat it.
in xcode create a new file and choose objective c class and set the class name to MyButton or any other name, and set the subclass to UIButton.
Pay attention that in the subclass the UIButton is the self property. so when you wish to set it's properties you should use the self. for example:
self.tag = 1;
[self setImage:...];
//etc
Then in you view controllers you create MyButton like this:
MyButton *button = [[MyButton alloc]initWithFrame:...];
[self addSubView:button];

iOS Storyboard How to access the controls and add event handlers, and binding data to controls added on Storyboard

I used to create the UI thru code. But now I have to use storyboard. I am confused about how to add the event handlers to the controls added on the storyboard and how to bind the data dynamically to the controls added on storyboard. A sample scenario is An UIView is added on the storyboard and two UITableViews and a button are added on top of it. I want to add event handler to the button and bind data to the table views. How do I do this. If I subclass the UIView added on the storyboard will I have access to the controls(button, two table views) added on top of the view or how else I should achieve this ?. Please help !
Adding an event handler to a button is relatively simple. In your UIViewController subclass simply add a method similar to the one below, then in your interface builder select the viewcontroller and on the right side panel click the right most button at the top which looks like an arrow pointing to the right. under received actions drag the circle to the button that you want to perform the action.
-(IBAction)doSomething:(id)sender{
//code for doing what you want your button to do.
}
A separate way you could do it, if you still want to do it programatically is to do the same thing you're used to doing, except in your .h file add IBOutlet UIButton *buttonName; and in the right pane under outlets you'll see your button. which can then be referenced by name within the .m file.

how to add UIImageView overlay over a UITableViewController based controller?

How does one in XCode XIB builder add a UIImageView to an existing MasterViewController that has a TableView and is based on NavigationController?
Scenario
For example if you create a new project based on MasterDetail in XCode 4
Then look at the MasterViewController.xib file - so want to add a UIImageView to this that could be used to overtake the screen up until a point after which the image view is made hidden to disappear.
I'm trying to drag a UIImageView onto the page, and putting it just able TableView in the Objects column, however this doesn't work. Doesn't even show the image.
So even just knowing how to drag-drop an image onto the MastViewController.xib such that it takes over the screen at startup would be a step forward (not sure if I need a view to encapsulate both the UIImageView and the TableView?)
Background
Want to have an intermediate image that the iPhone app shows as it starts up
Idea would be start up (static) image is show, then straight away go to MasterViewController which would show this same static image with an activity indicator on it
Then when (is a background thread) the data preparation is down, the UIImageView could be made non-visible
PS. Some additional notes:
Initially there is just a table view you see in MasterViewController.xib.
If I drag an imageview in at the same level as the TableView it doesn't appear.
If I create a new view in the XIB and put the tableview and uiimageview under this, I get an error when running "'-[UITableViewController loadView] loaded the "MasterViewController" nib but didn't get a UITableView.'"
Is there a view or just a table view in your xib? If you want both a image view and a table view in the same view controller, you should have an ordinary UIViewController and let in implement UITableViewDelegate and UITableViewDataSource protocols and add a table view and a image view to its view.
so solved by:
creating a top level "view" in the XIB
put the UITableView & the new UIImageView under this new view
but, had to change the interface line to inherit from UIViewController NOT UITableViewController
Just add Default.png in resource.

Add programmatically clickable Button with custom Views layouted in InterfaceBuilder

I have a UIScrollView.
In there I add buttons programmatically.
Right now I just use UIButton and set one title. But I need to have an UIImage View in there, that will change upon certain events, Text that will change too etc. and I would like to style that in Interface Builder, but I can't figure out how to do this. Which class should I extend, and what kind of xib should I create and what in there, so that in the end I can make the right connections and so that I can listen for UIControlEventTouchDown of that Button.
Just make a UIViewController subclass with a nib. You can do everything you want from there.

How to call a view on click of each UITableViewCell programmatically?

I have created a UITableViewcontroller and a UINavigationController in a TableController.m
with UITableviewCell set to say #"CellOne" #"CellTwo". Now I also created two other files
`ImageView1.m`
ImageView2.m
where if I click on CellOne I should be able to get the view placed on ImageView1.m, same applied to the ImageView2.m. How should I achieve this programmatically without using nib file?
override the touches event in ImageView1 and ImageView2.