I am trying to list out the selected index of many UISegmentControls. I set 5 of them on 1, 2, 3, 4, 5 respectively, and the result after using this code (in the array) is: 2, 2, 0, 0, 0...
for (UISegmentedControl *seg in segmentArray)
{
NSString* xWrapped = [NSString stringWithFormat:#"%d", seg.selectedSegmentIndex+1];
[difficultyH addObject: xWrapped];
}
Why??
EDIT:
This is how the segments are created:
//segment controll
NSArray *itemArray2 = [NSArray arrayWithObjects: #"very easy", #"easy", #"ok", #"hard", #"challenging", nil];
UISegmentedControl *segmentedControl2 = [[UISegmentedControl alloc] initWithItems:itemArray2];
segmentedControl2.frame = CGRectMake(480, -60, 130, 350);
segmentedControl2.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl2.selectedSegmentIndex = val - 1;
[segmentedControl2 addTarget:self action:#selector(segmentedControlHomework:) forControlEvents:UIControlEventValueChanged];
segmentedControl2.transform =
CGAffineTransformRotate(segmentedControl2.transform, degreesToRadians(90));
NSArray *arr = [segmentedControl2 subviews];
for (int i = 0; i < [arr count]; i++) {
UIView *v = (UIView*) [arr objectAtIndex:i];
NSArray *subarr = [v subviews];
for (int j = 0; j < [subarr count]; j++) {
if ([[subarr objectAtIndex:j] isKindOfClass:[UILabel class]]) {
UILabel *l = (UILabel*) [subarr objectAtIndex:j];
l.transform = CGAffineTransformMakeRotation(- M_PI / 2.0); //do the reverse of what Ben did
}
}
}
[image1 addSubview:segmentedControl2];
segmentedControl2.tag = i;
[segmentArray addObject: segmentedControl2];
//segment controll
Related
I have dynamic UISwitches that I create using the for loop. I give the tag value for each control to have an identity. I want to reach these values later, but I get 0 each time. What I'm doing wrong?
for(int i = 0;i < self.extraArray.count; i++) {
ProductPropertiesModel *model = (ProductPropertiesModel *)[self.extraArray objectAtIndex:i];
UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectZero];
switchButton.translatesAutoresizingMaskIntoConstraints = false;
switchButton.tag = [model Id];
[switchButton setOn:NO];
[self.view addSubview:switchButton];
[switchButton addTarget:self action:#selector(setState:)
forControlEvents:UIControlEventValueChanged];
}
-(void)setState:(id)sender
{
UISwitch *uiswitch = (UISwitch *)sender;
NSInteger tagInteger= uiswitch.tag;
NSLog(#"%#", [NSString stringWithFormat:#"%li",(long)tagInteger]);
}
The tag value is 0, but that is wrong.
I found the problem. I just made a typo. I noticed that I was writing a typo when I was taking json data. I noticed Id writing instead of id.
NSError *jsonError;
self.extraJsonArray = nil;
self.extraJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
self.extraArray = [[NSMutableArray alloc]init];
for(int j = 0;j < self.extraJsonArray.count;j++){
#try {
//NSInteger *Id = (NSInteger *)[(NSNumber *)[[self.extraJsonArray objectAtIndex:j]objectForKey:#"Id"] integerValue]; Problem is here
NSInteger *Id = (NSInteger *)[(NSNumber *)[[self.extraJsonArray objectAtIndex:j]objectForKey:#"id"] integerValue];
double price = (double)[(NSNumber *)[[self.extraJsonArray objectAtIndex:j] objectForKey:#"price"] doubleValue];
NSString *property =(NSString *)[[self.extraJsonArray objectAtIndex:j] objectForKey:#"property"];
ProductPropertiesModel *model = [[ProductPropertiesModel alloc] init];
[model setId:Id];
[model setProperty:property];
[model setPrice:price];
[model setChecked:#"No"];
[self.extraArray addObject:model];
} #catch (NSException *exception) {
NSLog(#"%#", exception.reason);
} #finally {
}
}
I would recommend instead setting [model id] as tag, you should set array index as tag.
and then later on in the switch control method, you can get the value from array index, using the code.
ProductPropertiesModel *model = (ProductPropertiesModel *)[self.extraArray objectAtIndex:i];
Hope this helps.
Try this:
for (int i =0; i < self.extraArray.count; i++) {
CGRect frame = CGRectMake(x, y, height, width);
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];
//add tag as index
switchControl.tag = i;
[switchControl addTarget:self action:#selector(setState:) forControlEvents: UIControlEventValueChanged];
[switchControl setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:switchControl];
y= y+50.0;
}
- (IBAction) setState: (UISwitch *) sender {
NSLog(#"no.%d %#",sender.tag, sender.on ? #"On" : #"Off");
//use sender.tag , you know which switch you selected
}
I am developing IOS App. I Create TextField and Button Dynamically and Set tag value.but problem is that when click button to get textfield first index value that show null. only last index value of textfield i am get not for other What is the problem. Thanks in Advance.
Code..
- (void)addfields{
_field = [[UITextField alloc]initWithFrame:CGRectMake(5.0f, 5.0f, 195.0f, 30.0f)];
_field.layer.borderColor=[[UIColor colorWithRed:211.0f/255.0f
green:211.0f/255.0f
blue:211.0f/255.0f
alpha:1.0] CGColor];
[_field.layer setBorderWidth: 1.0];
//_field.tag = count;
[_filterPossibleValueView addSubview:_field];
_addField = [[UIButton alloc]initWithFrame:CGRectMake(202.0f, 5.0f, 30.0f, 30.0f)];
_addField.backgroundColor = [UIColor blackColor];
//_addField.tag = count;
[_addField addTarget:self action:#selector(customFieldAdd:) forControlEvents:UIControlEventTouchUpInside];
[_filterPossibleValueView addSubview:_addField];
}
- (IBAction)customFieldAdd:(id)sender{
[_addfieldArray addObject:_field];
[_scroller setScrollEnabled:YES];
_scroller.contentSize=CGSizeMake(240.0f, _field.frame.size.height+x+50);
[_copyfield removeFromSuperview];
[copyAddButton removeFromSuperview];
x = 10;
y = 10;
for( int i = 0; i < [_addfieldArray count]; i++ ) {
_copyfield = [[UITextField alloc]initWithFrame:CGRectMake(5.0, x + _field.frame.size.height, 195.0f, 30.0f)];
_copyfield.layer.borderColor=[[UIColor colorWithRed:211.0f/255.0f
green:211.0f/255.0f
blue:211.0f/255.0f
alpha:1.0] CGColor];
_copyfield.tag = i;
[_copyfield.layer setBorderWidth: 1.0];
[_filterPossibleValueView addSubview:_copyfield];
x = x+_field.frame.size.height+10;
copyAddButton = [[UIButton alloc]initWithFrame:CGRectMake(202.0f, y + _addField.frame.size.height, 30.0f, 30.0f)];
copyAddButton.backgroundColor = [UIColor blueColor];
copyAddButton.tag = i;
[copyAddButton addTarget:self action:#selector(customFieldDelete:) forControlEvents:UIControlEventTouchUpInside];
[_filterPossibleValueView addSubview:copyAddButton];
y = y+_addField.frame.size.height+10;
count++;
}
}
- (IBAction)customFieldDelete:(id)sender{
UIButton *button = (UIButton*)sender;
NSInteger index = button.tag;
[_addfieldArray removeObjectAtIndex:index];
// UITextField *text = (UITextField *)[_copyfield viewWithTag:index];
// NSLog(#"%ld",(long)text.tag);
UITextField *txtField = [_filterPossibleValueView viewWithTag:index];
NSLog(#"%#",txtField.text);
// [_copyfield removeFromSuperview];
// [copyAddButton removeFromSuperview];
}
I have created an sample app for dynamic fields. And getting the textfield's value on Button click. This example code will solve out your problem.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
NSMutableArray *formArr;
NSMutableArray *txtfldArr;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
formArr = [[NSMutableArray alloc] init];
txtfldArr = [[NSMutableArray alloc] init];
UITextField *txtfld = [[UITextField alloc] init];
NSMutableDictionary *dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:#"string" forKey:#"labelName"];
[dict1 setObject:#"name" forKey:#"labelFor"];
[dict1 setObject:#"1" forKey:#"tag"];
[dict1 setObject:txtfld forKey:#"txtFld"];
[formArr addObject:dict1];
NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init];
[dict2 setObject:#"string" forKey:#"labelName"];
[dict2 setObject:#"email" forKey:#"labelFor"];
[dict2 setObject:#"2" forKey:#"tag"];
[dict2 setObject:txtfld forKey:#"txtFld"];
[formArr addObject:dict2];
NSMutableDictionary *dict3 = [[NSMutableDictionary alloc] init];
[dict3 setObject:#"string" forKey:#"labelName"];
[dict3 setObject:#"phone" forKey:#"labelFor"];
[dict3 setObject:#"3" forKey:#"tag"];
[dict3 setObject:txtfld forKey:#"txtFld"];
[formArr addObject:dict3];
NSMutableDictionary *dict4 = [[NSMutableDictionary alloc] init];
[dict4 setObject:#"string" forKey:#"labelName"];
[dict4 setObject:#"address" forKey:#"labelFor"];
[dict4 setObject:#"4" forKey:#"tag"];
[dict4 setObject:txtfld forKey:#"txtFld"];
[formArr addObject:dict4];
int y = 70;
for (int i = 0; i < [formArr count]; i++) {
NSString *txtfldName = [NSString stringWithFormat:#"%#",[[formArr objectAtIndex:i] objectForKey:#"labelFor"]];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(30, y+4, 80, 25)];
lbl.text = txtfldName;
lbl.textColor = [UIColor darkGrayColor];
[self.view addSubview:lbl];
UITextField *lbl_txtfld = [[UITextField alloc] initWithFrame:CGRectMake(120, y, 150, 30)];
lbl_txtfld.text = #"";
lbl_txtfld.delegate = self;
lbl_txtfld.textColor = [UIColor whiteColor];
lbl_txtfld.backgroundColor = [UIColor blackColor];
[self.view addSubview:lbl_txtfld];
[[formArr objectAtIndex:i] setObject:lbl_txtfld forKey:#"txtFld"];
y += 40;
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonClicked:(id)sender {
for (int i = 0; i < [formArr count]; i++) {
NSString *labelName = [NSString stringWithFormat:#"%#",[[formArr
objectAtIndex:i] objectForKey:#"labelFor"]];
UITextField *txtfld = [[formArr objectAtIndex:i]
objectForKey:#"txtFld"];
NSLog(#"txtfld value for %# = %#",labelName,txtfld.text);
}
}
Does anyone know why is appearing white part? My View is already gray, but gets two white pieces
whites: Arrow and final Popover!
[UPDATE]
this is the code that calls the popover and makes the arrow points to the button that was clicked!
- (void) buttonFilter {
if (viewFilter == #"Artistas") {
content = [self.storyboard instantiateViewControllerWithIdentifier:#"TipoArtistaViewController"]; // MUDAR PARA O NOVO FILTRO DE ARTISTAS
} else if (viewFilter == #"Músicas") {
content = [self.storyboard instantiateViewControllerWithIdentifier:#"CategoriaViewController"];
}
[self callFilter:btnFilter Filter:content];
}
- (void)callFilter:(id)sender Filter:(UIViewController *) content{
self.currentPop = popoverController;
popoverController = [[WYPopoverController alloc] initWithContentViewController:content];
UIButton * bt = (UIButton * )sender;
UIView *view = [bt valueForKey:#"view"];
popoverController.popoverContentSize = CGSizeMake(320, 180);
popoverController.delegate = self;
[popoverController presentPopoverFromRect:view.bounds inView:view permittedArrowDirections:WYPopoverArrowDirectionAny animated:YES];
}
the next is where to mount the session:
//extend and collpase
- (void)setupViewController {
categoriaBD = [categoriaDAO selectCategoria];
self.data = [[NSMutableArray alloc] init];
for (int i = 0; i < [categoriaBD count]; i++)
{
NSMutableDictionary * teste = [categoriaBD objectForKey:[NSString stringWithFormat:#"%i", i]];
ID = [[teste objectForKey:#"1"] integerValue];
subcategoriaBD = [categoriaDAO selectSubCategoriaByCategoriaID:ID];
NSMutableArray* section = [[NSMutableArray alloc] init];
for (int j = 0; j < [subcategoriaBD count]; j++)
{
NSMutableDictionary * subCat = [subcategoriaBD objectForKey:[NSString stringWithFormat:#"%i", j]];
[section addObject:[NSString stringWithFormat:[subCat objectForKey:#"1"]]];
}
[self.data addObject:section];
}
self.headers = [[NSMutableArray alloc] init];
for (int i = 0; i < [categoriaBD count]; i++)
{
NSString *inStr = [NSString stringWithFormat: #"%i", (int)i];
nomeCategoria = [categoriaBD objectForKey:inStr];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 310, 40)];
[label setText:[nomeCategoria objectForKey:#"2"]];
UIView* header = [[UIView alloc] init];
[header setBackgroundColor:[UIColor colorWithRed:(226/255.0) green:(226/255.0) blue:(226/255.0) alpha:1]];
[header addSubview:label];
[self.headers addObject:header];
}
}
You can to create a custom UIPopoverBackgroundView subclass that sets the properties of the arrow you want.
popoverController.popoverBackgroundViewClass = [MyPopoverBackgroundView class];
I’m developing word puzzle game,where one word is given.according to that word the random letters are generated…for that, i applied below logic but some how the random letters not generated as per the given word.
NSMutableArray *ArrOfABCD = [[NSMutableArray alloc]
initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z",
nil];
float x = 70;
float y = 100;
float width = 30;
float height = 20;
for(int i =0;i<32;i++)
{
NSUInteger randomIndex = arc4random() %(ArrOfABCD.count);
UIButton *btnLetter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnLetter setFrame:CGRectMake(x, y, width, height)];
[btnLetter setTitle:[NSString stringWithFormat:#"%#",ArrOfABCD[randomIndex]]
forState:UIControlStateNormal];
[self.view addSubview:btnLetter];
x = x + width + 30;
if((i+1)%4==0)
{
x = 70;
y = y + height + 20;
}
}
can any one suggest me that where i made mistake?
You should generate the unique random number
NSMutableArray* ArrOfWords = [[NSMutableArray alloc] initWithObjects:#"Good",#"Home",#"Beauty",#"Good",nil];
NSMutableArray *ArrOfABCD = [[NSMutableArray alloc] initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
NSMutableArray *arrDuplicate=[[NSMutableArray alloc]init];
float x = 70;
float y = 100;
float width = 30;
float height = 20;
NSInteger rowIndex=4;
NSString *dataString=[ArrOfWords objectAtIndex:0];
NSMutableArray *arrNumber = [[NSMutableArray alloc]init];
for (int i = 0; i < [dataString length]; i++) {
NSInteger index=arc4random_uniform(rowIndex)+rowIndex;
NSNumber *number=[NSNumber numberWithInt:index];
while ([arrDuplicate containsObject:number] ) {
NSInteger index=arc4random_uniform(rowIndex)+rowIndex;
number=[NSNumber numberWithInt:index];
}
[arrNumber addObject:number];
[arrDuplicate addObject:number];
}
[arrDuplicate removeAllObjects];
NSMutableArray *arrayChar = [NSMutableArray array];
for (int i = 0; i < [dataString length]; i++) {
[arrayChar addObject:[NSString stringWithFormat:#"%C", [dataString characterAtIndex:i]]];
}
for(int i =0;i<32;i++)
{
NSString *str=[ArrOfABCD objectAtIndex:arc4random_uniform(ArrOfABCD.count)];
if ([arrNumber containsObject:[NSNumber numberWithInt:i]] ) {
str=[arrayChar objectAtIndex:arrDuplicate.count];
[arrDuplicate addObject:str];
}
NSLog(#"%#",str);
UIButton *btnLetter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnLetter setFrame:CGRectMake(x, y, width, height)];
[btnLetter setTitle:str forState:UIControlStateNormal];
x = x + width + 30;
if((i+1)%4==0)
{
x = 70;
y = y + height + 20;
}
}
I have a method that is called once in a while. it creates a button and gives it a frame and adds it on the self.view. The problem is that I don't want these buttons to overlap, so I am using a counter (integer) to keep the count of how many there are. Sometimes the counter goes wrong of +-1 and two buttons appear overlapping.
This is the code:
in my .h
int CountNumberOfBluetoothDevices;
in the .m
- (void) browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info{
NSLog(#"%#", peerID.description);
CountNumberOfBluetoothDevices = CountNumberOfBluetoothDevices + 1;
NSArray *keys = [BluetoothDeviceDictionary allKeys];
for (NSUInteger k = keys.count; k > 0; k--) {
MCPeerID *key = keys[k - 1];
UIButton *btn = BluetoothDeviceDictionary[key];
if ([key.displayName isEqualToString:peerID.displayName]) {
[btn removeFromSuperview];
NSLog(#"REMOVE DOUBLE");
CountNumberOfBluetoothDevices--;
[BluetoothDeviceDictionary removeObjectForKey:key];
}
}
if (CountNumberOfBluetoothDevices == 1) {
BluetoothDeviceButton = [[UIButton alloc] initWithFrame:CGRectMake(130, -200, 55,55)];
}else if (CountNumberOfBluetoothDevices == 2){
BluetoothDeviceButton = [[UIButton alloc] initWithFrame:CGRectMake(240, -200, 55,55)];
}else if (CountNumberOfBluetoothDevices == 3){
BluetoothDeviceButton = [[UIButton alloc] initWithFrame:CGRectMake(130, -125, 55,55)];
}else if (CountNumberOfBluetoothDevices == 4){
BluetoothDeviceButton = [[UIButton alloc] initWithFrame:CGRectMake(240, -125, 55,55)];
}
Is there a better way to detect this?
Don't use upper case variable and/or property names.
Change CountNumberOfBluetoothDevices = CountNumberOfBluetoothDevices + 1; to countNumberOfBluetoothDevices++;
Use [keys count] instead of keys.count.
The -200 in CGRectMake(130, -200, 55,55)]; is very strange an looks like you have some issues with your view hierarchy. Why is it negative?
Instead of this
NSArray *keys = [BluetoothDeviceDictionary allKeys];
for (NSUInteger k = keys.count; k > 0; k--) {
MCPeerID *key = keys[k - 1];
...
}
I would do
for (MCPeerID *key in [bluetoothDeviceDictionary allKeys]) {
...
}