I have a source code where the main screen has 10 buttons (images buttons), each button is linked to sql database with tableview items, in the XIB I select which tag for each button, 1, 2, 3, 4..
I want make these buttons to scroll, how to do so?
I tried to convert the view into scrollview, then I have deleted the images, and created buttons in the scrollview, but now I do not know how to link each of the button to its tag in the tableview?
below is what I used:
#implementation mainViewController;
#synthesize master;
#synthesize model;
(void)viewWillAppear:(BOOL)animated{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);//You Can Edit this with your require
scrollview.pagingEnabled=YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
UIImageView* imageview1 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"c1#2x.png"]];
imageview1.frame=CGRectMake(18.0, 18, 285,50);
[scrollview addSubview:imageview1];
UIImageView* imageview2 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"c2#2x.png"]];
imageview2.frame=CGRectMake(18.0, 78, 285,50);
[scrollview addSubview:imageview2];
UIImageView* imageview3 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"c3#2x.png"]];
imageview3.frame=CGRectMake(18.0, 138, 285,50);
[scrollview addSubview:imageview3];
UIImageView* imageview4 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"c4#2x.png"]];
imageview4.frame=CGRectMake(18.0, 198, 285,50);
[scrollview addSubview:imageview4];
Assingn a tag to the button when you create it:
[buttonName setTag:1];
[buttonName addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
Then use:
-(void)aMethod:(UIButton *)button {
if ([button tag] == 1) {
...do something...
}
}
Hope this helps.
Related
ΩI've been trying to do this for like an hour now, nothing seems to work:
Things I've tried:
1) Doesn't work.
UIImage *image = [UIImage imageNamed:#"scr1_button_topbar.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[v addSubview:button];
UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithCustomView:v];
self.navigationItem.backBarButtonItem= forward;
2) Works, but looks REALLY stupid, it leaves the "Back" text there.
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"scr2_btnback"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
3) Button doesn't show
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"scr2_btnback"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
And setting the text to #""
4) Setting the left bar button item - Works, but that's a little cheating, I want the back button, not the left button.
UIImage *backImage = [UIImage imageNamed:#"scr2_btnback.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton] ;
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = backBarButtonItem;
Just got told, you can't edit the view of the backBarButtonItem... YEEY me.
I tried this code:
custom barbuttonitem:
-(id)initWithImage:(UIImage *)image title:(NSString*)title target:(id)target action:(SEL)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
button.titleLabel.shadowOffset = CGSizeMake(0, -1);
button.titleLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.5];
button.titleLabel.textColor = [UIColor redColor];
[button setTitle:title forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[view addSubview:button];
self = [[UIBarButtonItem alloc] initWithCustomView:view];
return self;
}
and used it:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"my_account_icon.png"] title:#"" target:self action:#selector(buttonClick:)];
But in ios 6 event not activate, ios 5 run nomal, please help me !!!!
UIBarButtonItems with a customView set will not fire actions.
Try the following:
-(id)initWithImage:(UIImage *)image title:(NSString*)title target:(id)target action:(SEL)action
{
//create whatever custom views you need
UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
UITapGestureRecognizer * gestureRec = [[UITapGestureRecognizer alloc] initWithTarget: target action: action];
[view addGestureRecognizer: gestureRec];
//add your custom subviews to the view
self = [[UIBarButtonItem alloc] initWithCustomView:view];
return self;
}
Adding gesture recognizer to the subview of the UIBarButtonItem worked for me, even if UIBarButtonItem is initialized with the custom view.
Good luck!
I have 10 or so buttons setup inside a method as follows:
#implementation MyViewController
UIButton *originalButton;
etc...
- (void)setupButtons
{
originalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[originalButton addTarget:self action:#selector(originalButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
originalButton.frame = CGRectMake(20.0, 30.0, 100.0, 39.0);
UIImage *buttonImage = [[UIImage imageNamed:#"originalreg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *buttonImageHighlight = [[UIImage imageNamed:#"originalregblue.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[originalButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[originalButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[self.view addSubview:originalButton];
etc…
}
I decided to pull the common code into another method for efficiencies:
- (void)setupButton:(UIButton *)myButton withSelector:(SEL)selector withX:(CGFloat)x withY:(CGFloat)y withRegImage:(NSString *)regImage withHighlightImage:(NSString *)highlightImage
{
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(x, y, 100.0, 39.0);
UIImage *buttonImage = [[UIImage imageNamed:regImage] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *buttonImageHighlight = [[UIImage imageNamed:highlightImage] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[myButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
}
…and call it as such:
- (void)setupButtons
{
[self setupButton:originalButton withSelector:#selector(originalButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:#"originalreg.png" withHighlightImage:#"originalregblue.png"];
etc...
}
THIS ALL WORKS except, one of my buttons is used to hide all the others. In the original setup, pressing the "Hide Buttons" button resulted in the other buttons being hidden. Now, they remain on the screen. Here's the code for that:
[self setupButton:hideButtonsButton withSelector:#selector(hideButtonsButtonWasPressed:) withX:20.0 withY:530.0 withRegImage:#"hidebuttonsreg.png" withHighlightImage:#"hidebuttonsregblue.png"];
- (void)hideButtonsButtonWasPressed:(id)sender
{
// hide the buttons
originalButton.hidden = YES;
originalButton.enabled = NO;
etc…
}
I've confirmed this method is being called and the setHidden/setEnabled calls are being executed.
Any pointers gratefully received!
Tony.
its because your method uses single instance all the time. look at the first line of your initialization
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
replace this line with
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
it will return new instance of button each time.
YOu have Declared button instance like following ,right.
IBOutlet UIButton *btn1, *btn2, *btn3;
Now create a new button with your method as you have done before, just assign that button to respected button object
btn1 = [self setupButton:originalButton withSelector:#selector(originalButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:#"originalreg.png" withHighlightImage:#"originalregblue.png"];
btn2 = [self setupButton:originalButton withSelector:#selector(duplicateButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:#"duplicatereg.png" withHighlightImage:#"originalregblue.png"];
btn3 = [self setupButton:originalButton withSelector:#selector(olderButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:#"olderreg.png" withHighlightImage:#"originalregblue.png"];
I am appending some text on my NSTextView on the screen and so I want user to be able to scroll in the view to see the entire log. But I am not able to have my text view scrolling. Below is my code. Any idea whats wrong here.
NSView *superview = [window contentView];
NSRect scrollViewFrame = NSMakeRect(10, self.window.frame.size.height / 2 - 200, 400, 400);
NSScrollView *scrollview = [[NSScrollView alloc]
initWithFrame:scrollViewFrame];
NSSize contentSize = [scrollview contentSize];
[scrollview setBorderType:NSLineBorder];
[scrollview setHasVerticalScroller:YES];
[scrollview setHasHorizontalScroller:NO];
[scrollview setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
NSRect textViewFrame = NSMakeRect(0, 0, contentSize.width, contentSize.height);
self.logTextView = [[NSTextView alloc] initWithFrame:textViewFrame];
[self.logTextView setMinSize:NSMakeSize(0.0, contentSize.height)];
[self.logTextView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[self.logTextView setVerticallyResizable:YES];
[self.logTextView setHorizontallyResizable:NO];
[self.logTextView setAutoresizingMask:NSViewWidthSizable];
[scrollview addSubview:self.logTextView];
[superview addSubview:scrollview];
This is fixed. I forgot to set the document view for my scrollview.
[scrollview setDocumentView:self.logTextView];
I have a UIView that I've made into a button that's added below my Table View:
myUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120,50)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setAutoResizingMask: [UIViewAutoResizingFlexibleWidth|UIViewAutoResizingFlexibleHeight];
[myButton setFrame:CGRectMake(20,0,80,40)];
myButton.titleLabel.textColor = [UIColor blackColor]; //Doesn't seem to work
[myButton setTitle:#"Test" forState:UIControlStateNormal];
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
[myButton setBackgroundImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[myUIView addSubView:myButton];
No matter what I try, the font always seems to be white. What can I do to change the font's color?
You want to use setTitleColor: forState:
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];