enable a segment controller view on View load - ios

i have a segment controller, now and on its tap on segment 1 , a view appears and on tap of second segment another view is show, just below this segment controller. now i want when the view on which this segment controller is present appears, the 1st segment controller view appears with that and the first segment controller button appears to be tapped, in advance.
i am able to get this thing done by showing my view in this manner. but i am doin unecessary thing.. please advise me some other way.
-(void)loadTest
{
if(!cabinInfoDetailsViewObj)
{
cabinInfoDetailsViewObj = [[CabinInfoDetailsView alloc]initWithFrame:CGRectMake(232, 188, 617,412)];
}
[self addSubview:cabinInfoDetailsViewObj];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_WhiteButton.png"] forSegmentAtIndex:0];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_GreyButton.png"] forSegmentAtIndex:1];
}
this is segment controller part
- (IBAction)CabinInfoSegmentButtonTapped:(id)sender
{
if(CabinInfoPopupSegmentControl.selectedSegmentIndex == 0)
{
if(!cabinInfoDetailsViewObj)
{
cabinInfoDetailsViewObj = [[CabinInfoDetailsView alloc]initWithFrame:CGRectMake(232, 188, 617,412)];
}
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_WhiteButton.png"] forSegmentAtIndex:0];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_GreyButton.png"] forSegmentAtIndex:1];
cabinInfoDetails.textColor = [UIColor blueColor];
cabinInfoLocate.textColor =[UIColor blackColor];
[cabinInfoDeckPlansViewObj removeFromSuperview];
[self addSubview:cabinInfoDetailsViewObj];
}
if(CabinInfoPopupSegmentControl.selectedSegmentIndex == 1)
{
if(!cabinInfoDeckPlansViewObj)
{
cabinInfoDeckPlansViewObj = [[CabinInfoDeckPlansView alloc]initWithFrame:CGRectMake(232, 188, 617,412)];
}
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_WhiteButton.png"] forSegmentAtIndex:1];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_GreyButton.png"] forSegmentAtIndex:0];
cabinInfoDetails.textColor = [UIColor blackColor];
cabinInfoLocate.textColor =[UIColor blueColor];
[cabinInfoDetailsViewObj removeFromSuperview];
[self addSubview:cabinInfoDeckPlansViewObj];
}

in my application,used this code :
//Give .h file UISegmentedControl *seg;
- void)viewDidLoad
{
seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Notification", #"Messages", nil]];
seg.tag = 1;
seg.segmentedControlStyle = UISegmentedControlStyleBar;
seg.frame = CGRectMake(1, 0, 318, 45);
[seg addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
seg.tintColor = [UIColor whiteColor];
seg.selectedSegmentIndex = 0;
seg.backgroundColor=[UIColor clearColor];
[seg setDividerImage:[UIImage imageNamed:#""] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[seg setImage:[UIImage imageNamed:#"notificationselected.png"] forSegmentAtIndex:0];
[seg setImage:[UIImage imageNamed:#"message.png"] forSegmentAtIndex:1];
[self.view addSubview:seg];
}
pragma mark - UISegmentedControl
- (IBAction)segmentAction:(id)sender
{
UISegmentedControl *segControll = (UISegmentedControl *)sender;
if (segControll.selectedSegmentIndex==0)
{
[segControll setImage:[UIImage imageNamed:#"notificationselected.png"] forSegmentAtIndex:0];
[segControll setImage:[UIImage imageNamed:#"message.png"] forSegmentAtIndex:1];
//right code for select the segmentcontroll.index=0
}
else if (segControll.selectedSegmentIndex==1)
{
[segControll setImage:[UIImage imageNamed:#"notification.png"] forSegmentAtIndex:0];
[segControll setImage:[UIImage imageNamed:#"messageselected.png"] forSegmentAtIndex:1];
//right code for select the segmentcontroll.index=1
}
}
i hope it will help you.

Related

UIButton set title color not working

I have created the array of UIButtons , having the same action,
if i click the first button, the first button Colour should be change, other button Colour should not be changed. how to achieve this?
for(titleStr in titleArray){
actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[actionButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
actionButton.tag = count;
[actionButton setTitle:titleArray[count] forState:UIControlStateNormal];
[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change
CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize];
CGRect buttonframe;
buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25);
actionButton.frame = buttonframe;
[titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]];
[contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]];
if(count==0){
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
[actionButton setSelected:YES];
tempObj = actionButton;
}else {
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
}
[titleScrollView addSubview:actionButton];
contentOffset += actionButton.frame.size.width+5.0;
titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height);
// Increase the count value
count++;
}
-(void)buttonTapped:(id)sender {
if([sender tag]==0){
UIButton *tappedButton = (UIButton *)sender;
[actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
actionButton.titleLabel.textColor = [UIColor whiteColor];
[actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
Thanks advance
for that you have to take one extra UIButton Object in you header file.
While user click on any button you have to store that selected Button in refbutton object and use like below.
#interface YourViewController ()
{
UIButton *btnSelected;
}
Now Move to your Button Action:
-(IBAction)buttonTapped:(UIButton *)sender {
if(_btnSelected){
_btnSelected.titleLabel.textColor = [UIColor blackColor];
}
_btnSelected = sender;
sender.titleLabel.textColor = [UIColor whiteColor];
}
Hope this will help you.

Select/Deselect for dynamically created UIButtons

I will explain the Scenario:
I am dynamically creating three UIButtons inside a for loop.The condition is by default the first button should be bordered.Now if we select other buttons the previous button's border should go and now the border should be present for selected button.
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
self.graphSubDataButton = [[UIButton alloc] init];
self.graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[self.graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[self.graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
self.graphSubDataButton.tag = btnsCount+1;
[self.graphSubDataButton addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
if (self.graphSubDataButton.tag==1)
{
[self.graphSubDataButton.layer setBorderWidth:1.2f];
[self.graphSubDataButton.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[self.graphSubDataButton.layer setCornerRadius:8.0f];
}
}
-(void) enableRespectiveButton2:(UIButton *) sender
{
}
I have changed your code structure little to make it work properly, you can comment if you don't understand something.
NSInteger defaultSelectedTag = 1;
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
UIButton *graphSubDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
graphSubDataButton.tag = btnsCount+1;
[graphSubDataButton addTarget:self action:#selector(enableRespectiveButton:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
//add your button somewhere
//e.g.
//[self.view addSubview:graphSubDataButton];
if (graphSubDataButton.tag == defaultSelectedTag) {
[self setBorderForButton:graphSubDataButton];
}
}
- (void) setBorderForButton:(UIButton *)sender {
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[sender.layer setCornerRadius:8.0f];
}
- (void) enableRespectiveButton2:(UIButton *) sender {
for(UIButton *buttons in [sender.superview subviews]) {
if([buttons isKindOfClass:[UIButton class]]) {
if(![buttons isEqual:sender]) {
//add logic to, remove borders
//buttons.layer.borderWidth = 0.0f;
}
}
}
[self setBorderForButton:sender];
}
Use tag to get current selected button or much simple: iterate through all subviews and set border to 0 for UIButtons
for (UIView *subview in [uiv_ButtonsView subviews]) {
if([subview isKindOfClass:[UIButton class]]) {
subview.layer setBorderWidth:0.0f;
}
}
then use:
UIButton *button=(UIButton *)sender;
[button.layer setBorderWidth:1.2f];
[button.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[button.layer setCornerRadius:8.0f];
Create dynamic buttons like :
int buttonsOffset = 50;
for (int btnsCount = 0 ; btnsCount <3; btnsCount++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor blueColor];
btn.frame = CGRectMake(buttonsOffset, 100, 120, 40);
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
btn.tag = btnsCount+1;
[btn addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + btn.frame.size.width;
if (btn.tag==1)
{
[btn.layer setBorderWidth:1.2f];
[btn.layer setBorderColor:[UIColor greenColor].CGColor];
[btn.layer setCornerRadius:8.0f];
self.graphSubDataButton = btn;
}
[self.view addSubview:btn];
}
And click event of button is as following
- (void)enableRespectiveButton2:(UIButton *)sender
{
sender.selected = TRUE;
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColor greenColor].CGColor];
[sender.layer setCornerRadius:8.0f];
self.graphSubDataButton.selected = FALSE;
[self.graphSubDataButton.layer setBorderWidth:0.0f];
[self.graphSubDataButton.layer setBorderColor:[UIColor clearColor].CGColor];
[self.graphSubDataButton.layer setCornerRadius:0.0f];
self.graphSubDataButton = sender;
}
Create dynamic buttons with for loop as shown below.
for(int i = 0 ;i <3; i++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
NSString * title = [NSString stringWithFormat:#"Button %d",i];
[btn setTitle:title forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"BorderImg"] forState:UIControlStateSelected];
[btn setBackgroundImage:[UIImage imageNamed:#"UnBordered"] forState:UIControlStateNormal];
if(i == 0) [btn setSelected:YES];
else [btn setSelected:NO];
[btn setTag:i];
[btn addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
}
Have 2 images
Highlighted border (BorderImg)
Normal border (UnBordered)
Set those images to button while creating in for loop
- (void)btnTapped:(UIButton *) btn {
if(! btn.isSelected) {
[btn setSelected:YES];
}
Inside button selector method, based on the sender selection state , update the button selected state. (In side selector method i.e btnTapped set other buttons selected state to false)

button Highlighted not work when interactivePopGestureRecognizer.enabled = YES

Use interactivePopGestureRecognizer when popViewController.
Set Custom back button and keep interactivePopGestureRecognizer = YES.
- (void)setNavigation {
[self.navigationController.scrollNavigationBar setNavigationTitleColor:[UIColor whiteColor]];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back"]
style:UIBarButtonItemStyleDone
target:self
action:#selector(popViewController)];
backButton.tintColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = backButton;
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
}
have a likeButton on the ViewController.
- (YMFeedLikeButton *)likeButton {
if (!_likeButton) {
YMFeedLikeButton *likeButton = [[YMFeedLikeButton alloc] init];
replyButton.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 50, 150, 50);
[likeButton setImage:[UIImage imageNamed:#"topic-icon-like.png"]
forState:UIControlStateNormal];
[likeButton setImage:[UIImage imageNamed:#"topic-icon-like.png"]
forState:UIControlStateNormal | UIControlStateHighlighted];
[likeButton setImage:[UIImage imageNamed:#"topic-icon-liked.png"]
forState:UIControlStateSelected];
[likeButton setImage:[UIImage imageNamed:#"topic-icon-liked.png"]
forState:UIControlStateSelected|UIControlStateHighlighted];
UIImage *highlight = [UIImage imageNamed:#"highlight.png"];
[likeButton setBackgroundImage:highlight
forState:UIControlStateHighlighted | UIControlStateSelected];
[likeButton setBackgroundImage:highlight
forState:UIControlStateHighlighted | UIControlStateNormal];
[likeButton setBackgroundImage:highlight
forState:UIControlStateHighlighted];
[likeButton addTarget:self action:#selector(like)
forControlEvents:UIControlEventTouchUpInside];
[likeButton setTitleColor:[UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:1.0f]
forState:UIControlStateNormal];
[likeButton setTitleColor:[UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:1.0f]
forState:UIControlStateSelected];
[likeButton setImageEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
likeButton.value = 0;
[self insertSubview:_likeButton = likeButton atIndex:0];
}
return _likeButton;
}
likeButton Highlighted is not working when i clicked it.
If close interactivePopGestureRecognizer
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
or change likeButton.frame = CGRectMake(150, 150, 150, 50);
,likeButton Highlighted touch is working.
i hope likeButton Highlighted is working when usedinteractivePopGestureRecognizer.
It's late, but anyway... This solution works for me:
self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.navigationController.interactivePopGestureRecognizer.cancelsTouchesInView = NO;
self.navigationController.interactivePopGestureRecognizer.delaysTouchesBegan = NO;
self.navigationController.interactivePopGestureRecognizer.delaysTouchesEnded = NO;

How to store all selected buttons values and show them on UILabel?

Actually I just want to make it like:
When user taps button 1 , it stores its value in a string and when user selects another button; it also store its value also and when he de-select the button again it must be removed from string and also from uiLabel
How it will be possible ?
I have to know all possible ways
1) Declare in your ViewController.m
#interface ViewController ()
{
UIButton *button1;
UIButton *button2;
NSString *btn1String;
NSString *btn2String;
BOOL btn1isClicked;
BOOL btn2isClicked;
UILabel *label;
}
2) Buttons and label (can be in your ViewDidLoad)
btn1isClicked = NO;
btn2isClicked = NO;
//initialy set to nothing
btn1String = #"";
btn2String = #"";
//create first button
button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 50, 200, 50)];
//button 1 clicked
[button1 addTarget:self action:#selector(button1Clicked) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Button One" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[self.view addSubview:button1];
//create second button
button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 150, 200, 50)];
//button 2 clicked
[button2 addTarget:self action:#selector(button2Clicked) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:#"Button Two" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:button2];
//create label
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 320, 50)];
[label setText:#""];
[self.view addSubview:label];
3) Button Clicked methods
- (void) button1Clicked
{
if (!btn1isClicked)
{
//set bool to yes
btn1isClicked = YES;
[button1 setTitle:#"Button One Clicked" forState:UIControlStateNormal];
//set button 1 value
btn1String = #"btn1value";
}
else
{
//set bool to no
btn1isClicked = NO;
[button1 setTitle:#"Button One" forState:UIControlStateNormal];
btn1String = #"";
}
//update label
[label setText:[NSString stringWithFormat:#"%# %#", btn1String, btn2String]];
}
- (void) button2Clicked
{
if (!btn2isClicked)
{
btn2isClicked = YES;
[button2 setTitle:#"Button Two Clicked" forState:UIControlStateNormal];
//set button 2 value
btn2String = #"btn2value";
}
else
{
btn2isClicked = NO;
[button2 setTitle:#"Button Two" forState:UIControlStateNormal];
btn2String = #"";
}
//update label
[label setText:[NSString stringWithFormat:#"%# %#", btn1String, btn2String]];
}
Hope this helps
-(IBActions)buttonDidTap:(UIButton*)btn { //the target selector the button is attached to
if ([btn isSelected]) { //check if the button is selected
NSString *buttonTitleValue = btn.currentTitle; //Get the button title value
myLabel.text = buttonTitleValue; //set the UIlabel text to the title of the button
}
}
See: https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instp/UIButton/currentTitle

how to load same tableview with different NSArrays depending on uibuttons tapped?

In my app i have a scroll-view and a table-View added as a sub-view and on this scroll view i have multiple menu buttons (UI Button) added as sub-view to my scroll-view.i want to load the table-View with different arrays depending on respective menu button is tapped.Any Ideas?
- (void)viewDidLoad
{
[super viewDidLoad];
[self PutButtoninScrollView:7];
}
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
myButton1=[[UIButton alloc]init];
myButton1= [UIButton buttonWithType:UIButtonTypeCustom];
myButton1.frame=CGRectMake(5, 5, 70, 30);
[myButton1 setTitle:#"दुनिया" forState:UIControlStateNormal];
[myButton1 setTag:1];
myButton1.backgroundColor=[UIColor blackColor];
[myButton1 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton1];
myButton2=[[UIButton alloc]init];
myButton2= [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2.frame=CGRectMake(80, 5, 70, 30);
[myButton2 setTitle:#"India" forState:UIControlStateNormal];
[myButton2 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton2];
[self.myScrollView setContentSize:CGSizeMake(160, 35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}
-(void)ListViewAction:(id)sender
{
NSLog(#"sucessful");
if ([myButton.currentTitle isEqualToString:#"दुनिया"])
{
NSLog(#"successful again 1");
}
else if ([myButton.currentTitle isEqualToString:#"भारत"])
{
NSLog(#"successful again 2");
}
}
problem is when i execute the code
"successful" is displayed. that means ListViewAction method is getting called but my if statement is never executed.
try this
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
xPosition=5;
for(int i=0;i<numberOfbuttons;i++)
{
myButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame=CGRectMake(xPosition, 5, 70, 30);
myButton.backgroundColor=[UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
[myButton setTag:i];
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton];
xPosition+=75;
}
[self.myScrollView setContentSize:CGSizeMake(xPosition,35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}
- (void) buttonClicked:(id) sender
{
NSString *strTitle = [sender currentTitle];
// arrItem have list of array items
NextView *detailViewController = [[NextView alloc] initWithNibName:#"NextView" bundle:nil];
detailViewController.arrTableView =[arrItem objectAtIndex:[strTitle integerValue]];
[self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)ListViewAction:(id)sender
{
NSLog(#"sucessful");
if ([[sender currentTitle] isEqualToString:#"दुनिया"])
{
NSLog(#"successful again 1");
}
else if ([[sender currentTitle] isEqualToString:#"भारत"])
{
NSLog(#"successful again 2");
}
}
try this
[myButton1 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[myButton2 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];

Resources