Objective c 指针在传递到方法时不更新?

Objective c 指针在传递到方法时不更新?,objective-c,cocoa-touch,Objective C,Cocoa Touch,传递到“menuItem”方法的指针是alloc,对于“内部指针”,将注册一个值。但是对于“外部指针”,该值为“null”。。。为什么?我传入了指针,它应该在方法中修改吗 在头文件中: UIButton *bMenu_time; UILabel *lMenu_time; 实施: - (void) menuItem: (UIView*)vMenu menuButton:(UIButton*)bMenu menuLabel: (UILabel*)lMenu menuPosX: (double)

传递到“menuItem”方法的指针是alloc,对于“内部指针”,将注册一个值。但是对于“外部指针”,该值为“null”。。。为什么?我传入了指针,它应该在方法中修改吗

在头文件中:

UIButton *bMenu_time;
UILabel *lMenu_time;
实施:

- (void) menuItem: (UIView*)vMenu  menuButton:(UIButton*)bMenu menuLabel: (UILabel*)lMenu  menuPosX: (double)posX   menuLenX: (double)lenX  menuTagNum: (int)tagNum menuText: (NSString*)txtMenu{

    bMenu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [bMenu setFrame:CGRectMake(posX,0,lenX,25)];
    [bMenu setTag: tagNum];
    [bMenu addTarget:pSelf action:@selector(NewNumber:) forControlEvents:UIControlEventTouchUpInside];
    [vMenu addSubview:bMenu];


    lMenu = [[UILabel alloc] initWithFrame:CGRectMake(posX,0,lenX,25)];
    [lMenu setBackgroundColor:[UIColor lightGrayColor]];
    [lMenu setText:[NSString stringWithFormat:  txtMenu]];
    [lMenu setFont:[UIFont systemFontOfSize:14 ]];
    [lMenu setTextAlignment:UITextAlignmentCenter];
    [vMenu addSubview: lMenu];

    NSLog(@"\nPointer Inside: %@\n", lMenu); // <--------- INSIDE WORKS
}



- (void) menuBuild{     
    pSelf = self;
    theString = @"";


    vMenu = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,25)];
    [pSelf.view addSubview:vMenu];
    [vMenu setBackgroundColor:[UIColor grayColor]]; 


    iTime = 2;
    [self menuItem:vMenu menuButton:bMenu_time menuLabel:lMenu_time menuPosX:240+20 menuLenX:60 menuTagNum:102  menuText:[NSString stringWithFormat:  @"Hold: %d", iTime]];

    NSLog(@"\nPointer Outside: %@\n", lMenu_time); // <----- OUTSIDE is NULL ??
}
-(void)menuItem:(UIView*)vMenu menuButton:(UIButton*)bMenu menuLabel:(UILabel*)lMenu menuPosX:(double)posX menuLenX:(double)lenX menuTagNum:(int)tagNum menuText:(NSString*)txt菜单{
b菜单=[UIButton按钮类型:UIButtonyPeroundRect];
[bMenu setFrame:CGRectMake(posX,0,lenX,25)];
[bMenu setTag:tagNum];
[bMenu addTarget:pSelf action:@selector(NewNumber:)for controlEvents:UIControlEventTouchUpInside];
[vMenu addSubview:bMenu];
lMenu=[[UILabel alloc]initWithFrame:CGRectMake(posX,0,lenX,25)];
[lMenu setBackgroundColor:[UIColor lightGrayColor]];
[lMenu SETEXT:[NSString stringWithFormat:txtMenu]];
[lMenu setFont:[UIFont systemFontOfSize:14]];
[lMenu setTextAlignment:UITextAlignmentCenter];
[vMenu添加子视图:lMenu];

NSLog(@“\nPointer Inside:%@\n”,lMenu);//目标C与C一样,都是按值传递的。这意味着,如果要通过将指针传递给函数来更改指针,则需要将指针传递给该指针并使用它

在C中,这类似于:

void alloc128 (void **ptr) {
    *ptr = malloc (128);
}
将其映射到您的具体案例,您可以:

  • 修改
    menuBuild
    以传递要更改的内容的地址
  • 修改函数以接收指向指针值的指针
  • 修改函数以取消对指向点值的指针的引用,从而正确设置它们

paxdiablo是绝对正确的,但是如果您不确定传递引用的语法,那么您的代码应该是:

- (void) menuItem: (UIView*)vMenu  menuButton:(UIButton **)bMenu menuLabel: (UILabel **)lMenu  menuPosX: (double)posX   menuLenX: (double)lenX  menuTagNum: (int)tagNum menuText: (NSString*)txtMenu{

    *bMenu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [*bMenu setFrame:CGRectMake(posX,0,lenX,25)];
    [*bMenu setTag: tagNum];
    [*bMenu addTarget:pSelf action:@selector(NewNumber:) forControlEvents:UIControlEventTouchUpInside];
    [vMenu addSubview:*bMenu];


    *lMenu = [[UILabel alloc] initWithFrame:CGRectMake(posX,0,lenX,25)];
    [*lMenu setBackgroundColor:[UIColor lightGrayColor]];
    [*lMenu setText:[NSString stringWithFormat:  txtMenu]];
    [*lMenu setFont:[UIFont systemFontOfSize:14 ]];
    [*lMenu setTextAlignment:UITextAlignmentCenter];
    [vMenu addSubview: *lMenu];

    NSLog(@"\nPointer Inside: %@\n", *lMenu); // <--------- INSIDE WORKS
}



- (void) menuBuild{     
    pSelf = self;
    theString = @"";


    vMenu = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,25)];
    [pSelf.view addSubview:vMenu];
    [vMenu setBackgroundColor:[UIColor grayColor]]; 


    iTime = 2;
    [self menuItem:vMenu menuButton:&bMenu_time menuLabel:&lMenu_time menuPosX:240+20 menuLenX:60 menuTagNum:102  menuText:[NSString stringWithFormat:  @"Hold: %d", iTime]];

    NSLog(@"\nPointer Outside: %@\n", lMenu_time); // <----- OUTSIDE is NULL ??
}
-(void)menuItem:(UIView*)vMenu menuButton:(UIButton**)bMenu menuLabel:(UILabel**)lMenu menuPosX:(double)posX menuLenX:(double)lenX menuTagNum:(int)tagNum menuText:(NSString*)txt菜单{
*b菜单=[UIButton按钮类型:UIButtonyPeroundRect];
[*bMenu setFrame:CGRectMake(posX,0,lenX,25)];
[*bMenu setTag:tagNum];
[*bMenu addTarget:pSelf action:@selector(NewNumber:)for controlEvents:UIControlEventTouchUpInside];
[vMenu添加子视图:*bMenu];
*lMenu=[[UILabel alloc]initWithFrame:CGRectMake(posX,0,lenX,25)];
[*lMenu setBackgroundColor:[UIColor lightGrayColor]];
[*lMenu SETEXT:[NSString stringWithFormat:txtMenu]];
[*lMenu setFont:[UIFont systemFontOfSize:14]];
[*lMenu setTextAlignment:UITextAlignmentCenter];
[vMenu添加子视图:*lMenu];

NSLog(@“\nPointer-Inside:%@\n”,*lMenu);//好的……这是有效的。但是我传入了指针。为什么使用双指针?同时,我收到大量警告:不兼容的指针类型将“UILabel**”发送到“UILabel*”类型的参数要消除这些警告,您需要更新.h中的方法定义,以使.m与**匹配,用于menuButton和MenuLabel双指针的原因是paxdiablo试图解释的按值传递的内容。当您传递指针时,指针为nil,然后为其分配一个对象,指针需要更改为不是零。由于指针是按值传递的,因此不能将指针从零更改为非零。如果要更改原始指针本身的值,则必须将指针传递到指针。将指针视为整数-如果将整数传递给函数,您希望函数能够更改其值吗?不-您希望必须传递一个指向整数的指针!我以前传递过指针,但没有传递额外的星号。但是它们以前工作过,因为我以前做过内存分配。函数中的值已更新。那么,为什么没有双指针就不能进行分配呢?当您更改对象的属性时,它不会更改其指针。W当你创建一个对象时,它会改变它的指针。想想看,你有一个指向一个对象的指针。指针是内存中一个位置的数字。你在对象上设置了一个属性,它会改变对象的内部结构,但它不会将它移动到内存中的其他位置,所以指针不需要改变。但是如果你没有对象但是,指针是nil。如果将nil指针传递给函数,则只是将文本值零传递给方法。不能更改值零。