1

I stored some text value in server. I successfully fetched that text value and stored to local database file. Then added that values to array. when i display that array values to UITextView. But textview displaying all the values. I have button to click index. If i click first button i need to display first text. Then next for next button. If i click every button, it displays all the values.

code:

-(void)dbClicked:(id)sender{


    [mmageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[productimg_array objectAtIndex:[sender tag]-1]]]]];


    [self.view addSubview:mmageView];
UITapGestureRecognizer *singletapp=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    singletapp.numberOfTapsRequired=1;

    [mmageView addGestureRecognizer:singletapp];

}



- (void)singleTap:(UIGestureRecognizer *)singletap {

        UITextView *txt=[[UITextView alloc]initWithFrame:CGRectMake(50, 50, 150,150)];
                NSMutableString *string = [NSMutableString string];    

            for(int i=0;i<[descript_array count];i++){

                NSLog(@"descript array is  is %d",i);

      txt.text = [descript_array objectAtIndex:i];


              }

            [self.view addSubview:txt];

}

NSLog:

descript array is  is 14

string is the name is  image with black color
the name is image with black color
the name is image with white color
the name is 
the name is 
the name is 
the name is 
the name is 
the name is 
the name is 
the name is 
the name is 

descript array is  is 15

    string is the name is  image with black color
    the name is image with black color
    the name is image with white color
    the name is 
    the name is 
    the name is 
    the name is 
    the name is 
    the name is 
    the name is 
    the name is 
    the name is 
0

2 Answers 2

1
- (void)viewDidLoad
{
    UITextView *txt=[[UITextView alloc]initWithFrame:CGRectMake(50, 50, 150,150)];
    [self.view addSubview:txt];

    NSString *str_text;

    for(int i=0;i<[descript_array count];i++)
    {
        str_text = [NSString stringWithFormat:@"%@ %@",str_text,[descript_array objectAtIndex:i]];
        //[str_text retain];
    }
    txt.text = str_text;
    txt.editable=FALSE;

}
Sign up to request clarification or add additional context in comments.

5 Comments

wahooo...you are retaining the str_text AS MANY TIMES as the no. of elements in the array!! For 1K entries there will be 1K retains!!
then str retain out in loop
@BhaveshNai no need to reatin str.Yes it retain out in loop.If you are reatining make a way to release.From above code it seems that much memory r wasting.
Im getting str is (null) image with black color the name is image with black color the name is image with white color descript is ( image with black color the name is image with black color the name is image with white color "", "", "", "", "", "", "", "", "",
@user2674668 make str alloc init.,,
0

It's because of the For Loop. You just need to pull out the value rather calling a For loop here, like:

txt.text = [descript_array objectAtIndex:i];

All you need is to differentiate the Button Action call i.e:- Which button was pressed and which element to extract from the Array. So to differentiate between calls, all you need is to set a Tag to image upon which Gesture is implemented. The tag will help you to differentiate the gesture call, like:

 [mmageView setTag: 10]; //Let say 10

And in the Gesture delegate method:

- (void)singleTap:(UIGestureRecognizer *)sender {

       if (sender.tag == 10) 
       {
             UITextView * yourTextView=[[[UITextView alloc]initWithFrame:CGRectMake(50, 50, 150,150)] autorelease];

             yourTextView.text = [descript_array objectAtIndex:10];  //give appropriate index to extract value

       }
            [self.view addSubview:txt];

}//End of method

7 Comments

@user2674668: basically you have used loop to displays all the values. And ofcourse, in a For Loop every value will be displayed! If you need to display just one value then you need to extract value like i have answered!
@user2674668: You said " I have button to click index. If i click first button i need to display first text. Then next for next button. If i click every button, it displays all the values." .. Can you edit your question and put that IBActions inside your code so that i can guide you further?
@user2674668: yup i have seen. You need to Put the Button click (IBAction) code. I mean how are you performing button action? write that code
If i use txt.text = [descript_array objectAtIndex:i]; textview display only last value from array..
@user2674668: How many buttons you have on screen? can u post a sample image of what are you trying to achieve? Because still i am not clear why are you using Gestures instead of implementing simple IBActions for button click?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.