开发者

Cant access an int value

i declared an int value as my imageIndexForSend through the following code in myView class.

int imageIndexForSend; 
@property int imageIndexForSend;
@synthesize imageIndexForSend;

after on a button click i am displaying a popover which is PopOver calss. there is table view with multiple indexes in popover class.when i click on any row in PopOver class table it set myView class imageIndexForSend as

In PopOver

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    myView *obj = [[myView alloc] init];
    [obj getImageForSend:indexPath.row];
    [staticSceneController release];
}

in myViewClass

-(void)getImageForSend:(int)index{
    imageIndexForSend = index;
}

then i am return to myViewClass after dismissing popover (popOver class) and doing开发者_开发技巧 some actions in myViewClass.

then i am clicking a send button.but the integer value imageIndexForSend is zero.cant get the old value which i set from PopUp.

can any one tell me a way to get the old imageIndexForSend value.may i know what mistake i done.


First, you name a method with get to set the value, it's bad.

Second, you use a property and synthesize it, so you don't need to rewrite the set method unless you need to have a custom set method.

And finally you create a new view on each selection of tableview cell !

1) Remove your getImageForSend: method, you don't need that with property

2) Instead using : [obj getImageForSend:indexPath.row];, use : obj.imageIndexForSend = indexPath.row;

3) Don't create a new view on each selection, assign the value on the existing view.

A better way to transmit data from your popover to your view (controller ?) is to have a delegate property in your popover class and set it with your view object, create a delegate protocol with a method that is called when a cell is selected in popover with an int argument (the index) then make your view class adopts the protocol and do a obj.imageIndexForSend = argument; in your protocol method.


It seems you're allocating a myView instance and assigning that to a local variable (obj), but then you don't keep a pointer to that new instance anywhere.

From what I understand, you already have an existing instance of myView, so what you need to do is to set the variable on that instance, and not create a new one every time.

Each instance have their own set of variables, so changing it in a new instance won't affect any other instances.


You are instantiating a new MyView whenever the user taps on any row of your UITableView. You should try to access the original MyView instead (or whatever object shall retain that setup value).

Within your popover, you should find a way to access the instance that holds the actual index-value. How exactly that is achieved depends a lot on your implementation.

In other words, do not instantiate something within an object that has a shorter lifetime than the object that will access that very instance.


If you're trying to access the index of the selected row in the UITableView you can just use the following:

int index = [myTableView indexPathForSelectedRow].row;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜