Making an assignable grid to segue in xcode - iphone

I need to implement a grid view of images that requires each segment to segue to another view controller /or View.
My parameters:
I need 260 segments: approx 18px x 18px
Each segment will be numbered (1-260) and have a diffrent (background) image
One segment must be highlighted (a daily square like ical)
You can tap any segment to segue to the next view controller
I have looked at custom TVCells / Buttons but are there more options out there?
Thank you

In the end I opted for:
Linking the 1stViewController to the 2ndViewController in the
storyboard and naming the seque.identifier in the attributes.
Creating a grid of UIButtons in IB and assigning them individual tags in the attributes (1-260 - don't use 0 as it is a default).
To change the buttons' backgroundImage daily I set up a day-counter integer
and in the viewDidLoad of the 1stVC coded:
[(UIButton*)[self.view viewWithTag:dayCount] setBackgroundImage:[UIImage imageNamed:#"image_Day.png"] forState:UIControlStateNormal];
As there were multiple UIButtons, I decided that dragging them all to a IBAction in IB was too long a task so I assinged them programatically using:
-(void) assignButtons{
[(UIButton*)[self.view viewWithTag:1] addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[(UIButton*)[self.view viewWithTag:2] addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
} //etc for all 260
Then used performSegueWithIdentifier: in a method:
-(IBAction) buttonClicked:(id)sender{
[self performSegueWithIdentifier:#"mySegue" sender:self];
}

Related

UIButton tapable area outside of subview

So I have a UIView which I call ctrView and this ctrView is added to my UIViewController's view. It's basically just a smaller rectangular frame inside. And I added a UIButton on the top right corner of this container view, however it seems that when clicking on the UIButton area which is a bit outside of the ctrView doesn't trigger the action tied to the button. Only the UIButton area that is inside the container view triggers the action. How do I resolve this weird issue?
UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[closeButton setImage:[UIImage imageNamed:#"close.png"] forState:UIControlStateNormal];
[closeButton setBackgroundColor:[UIColor clearColor]];
[closeButton addTarget:self action:#selector(closeZoomedImageView) forControlEvents:UIControlEventTouchUpInside];
[closeButton setCenter:CGPointMake(ctrView.frameX + ctrView.frameWidth - 30, ctrView.frameY - 15)];
[ctrView addSubview:closeButton];
[closeButton release];
This is the normal behavior, views outside the bound of the parent will not receive touch actions
In order to fix this, rearrange the views like following
Main view will be big enough to accomodate the internal view and the closebutton, the backGroundColor of this view will be clear color
Internal view will be the view that will the internal view that will have the image and or the content of your normal view
Main view will have both the internal view and the close button as its childern
Main view will be big enough to accomodate the button
Main view will have a clear color background so that it will not affect the layout
So you will add all the views you currently have inside the internal view,
The internal view and the button view will both be added to the main view
you present then the main view to the viewcontroller

Small UIButton on top of larger UIButton

I have a smaller UIButton which is on top of a larger UIButton.
The problem right now is that if I tap the smaller UIButton, it also will trigger the larger UIButton. The code I'm using to determine if the buttons were tapped is:
if(CGRectContainsPoint(button1.frame, location)) {
}
Is there a property of the buttons or some automated way to make the smaller button not effect the larger button?
I know I could alter the code above to say if its within button1's frame and not within button2, but is there another way to do it?
UIControl (which is the superclass of UIButton) passes itself as the only parameter to its target using the action selector. Make use it, it's there for exactly these cases!
[smallButton addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
[bigButton addTarget:self action:#selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
// ...
- (void) doStuff:(UIButton *)btn
{
if (btn == smallButton)
{
// smaller button was clicked
}
else
{
// bigger button was clicked
}
}

Button with hidden menu iPhone

I need to implement a single button which would be shown at upper right corner of the application on UIView or MKMapView. On clicking that button a combo should come up and user would be able to select the categories.
How can I achieve that?
You have to create an UIButton and add it as a subview of your UIView (for example in viewDidLoad method if your view is linked to an UIViewController).
UIButton *showButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showButton.frame = CGRectMake(500, 20, 150, 44); // hardcoded frame, not quite elegant but works if you know the dimension of your superview
[showButton setTitle:#"Show Categories" forState:UIControlStateNormal];
// add target and actions
[showButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a superview, your parent view
[superView addSubview:showButton];
Then you add a method, called buttonClicked: that takes an id parameter (usually the sender, showButton in this case).
-(void)buttonClicked:(id)sender
{
// visualize categories
}
To visualize the categories you can follow two different ways:
Present a UITableViewController inside a UIPopoverController (only for iPad device)
Show a modal controller presenting a UITableViewController (both iPad and iPhone devices).
The UITableViewController allows you to have a list of categories and then select one of them.
P.S. Check the code in XCode because I've written by hand (without XCode)

How to remove background image to a button

I added 10 buttons to a view (example view name is "menuView"), now I want to remove the background image for 2nd, 3rd, 4th buttons. I wrote the code like this
for(id btn in [menuView subViews]){
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
The problem with this code is, it is removing all 10 button's backGroundimage, but I need to set nil for the 2nd, 3rd, and 4th buttons
If you create a tag for the buttons that you add, you can filter against them.
for(UIButton *btn in [menuView subViews]){
if (btn.tag == 2 || btn.tag == 3 || btn.tag == 4) {
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
}
Of course, you need to make sure that there are no other views in menuView that could share the same tag. So the choices are to make the tags large, unique values, or checking that they are actually UIButtons. I've edited this assuming that the only subviews of menuView are UIButtons. Enumerating over UIButtons won't cause compiler warnings about tag not being a property of NSObject.
UIButton is a subclass of UIControl which is a subclass of UIView. UIView has the tag property, so UIButton inherits this property. It's useful to look at the docs for a class that you are using, and continue up the hierarchy to see if there are properties or methods that are useful for what you need to do.
Just to expand on my comment.
Using an IBOutletCollection which you can point an array to many objects in the nib. You declare this as such (synthesizing in the implementation):
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *threeButtons;
This says to IB that it's a collection of UIButton elements. In IB, you connect this to the three buttons you wish to remove the background image for, by control dragging from it to the buttons. Once this is done the array will contain those buttons you connected up and you can loop like so:
for (UIButton *button in self.threeButtons) {
[button setBackgroundImage:nil forState:UIControlStateNormal];
}
Again, the link to a more detailed explanation can be found at: http://bobmccune.com/2011/01/31/using-ios-4s-iboutletcollection
When creating the buttons, try using the "tag" property. Then, when you're setting the background to nil, you could check for btn.tag == 2,btn.tag == 3 or btn.tag == 4.
You could have assigned tags to the buttons from 1 to 10 when adding them to menu view. And now with the help of tags, we can decided what to do with buttons.
Firstly, are you placing the buttons using Interface Builder?
If so, I'd recommend placing numbered tags for each of the buttons and then you can use something like the following to find the appropriate buttons and remove the background image.
for(UIButton *buttonname in [yourView subViews]){
if (buttonname.tag == 2 || buttonname.tag == 3 || buttonname.tag == 4) {
[buttonname setBackgroundImage:nil forState:UIControlStateNormal];
}
}
If you're creating them programmatically and sequentially, I'd recommend placing the buttons in an array as they're made and just remove the background of the buttons using "objectAtIndex".

UITextField leftView

Is it possible to set up a UITextField with a leftView so that if a user clicks into the UITextField the keyboard shows but if they click on an icon in the leftView another method is called (i.e., one that displays a UIPickerview)?
Have you checked that the leftFieldViewMode is set to something other than never
[textField setLeftViewMode:UITextFieldViewModeAlways];
Instantiate a button as u do normaly
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,0,30,30)];
[btn setTitle:#"title" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(BtnClick) forControlEvents:UIControlEventTouchUpInside];
Instantiate a textfield as you do normally
UITextField *txtfield1=[[UITextField alloc]initWithFrame:CGRectMake(10, 10,300,30)];
[txtfield1 setBackgroundColor:[UIColor grayColor]];
[txtfield1 setPlaceholder:#" Enter"];
To set a view in left side(there is right view also available)
[txtfield1 setLeftView:btn];
if u want the view to show always then below code does it
[txtfield1 setLeftViewMode:UITextFieldViewModeAlways];
if u want the view to show never then
[txtfield1 setLeftViewMode:UITextFieldViewModeNever];
if u want the view to show if the textfield is not edited then below
[txtfield1 setLeftViewMode:UITextFieldViewModeUnlessEditing];
if u want the view to show if the textfield is edited then
[txtfield1 setLeftViewMode:UITextFieldViewModeWhileEditing];
button click event and pop over
-(void)BtnClick
{
//Usual pop over coding goes here
}
Have you tried using a UIButton for this task? From the documentation for the leftView property:
If your overlay view does not overlap
any other sibling views, it receives
touch events like any other view. If
you specify a control for your view,
the control tracks and sends actions
as usual. If an overlay view overlaps
the clear button, however, the clear
button always takes precedence in
receiving events.
So create a UIButton instance, configure its appearance and actions as needed, then set it as the leftView property.