English 中文(简体)
UIButton @selector 检测右纽,做错其他吗?
原标题:Weirdest occurrence ever, UIButton @selector detecting right button, doing wrong else_if ?

因此,我生动地建立了3个UIButtons(现今),这个地方:

NSMutableArray *sites = [[NSMutableArray alloc] init];

     NSString *one = @"Constution Center";
     NSString *two = @"Franklin Court";
     NSString *three = @"Presidents House";

     [sites addObject: one];
     [one release];

     [sites addObject: two];
     [two release];

     [sites addObject: three];
     [three release];

     NSString *element;
     int j = 0;
     for (element in sites)
     {
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

         //setframe (where on screen)
         //separation is 15px past the width (45-30)
         button.frame = CGRectMake(a, b + (j*45), c, d);

         [button setTitle:element forState:UIControlStateNormal];

         button.backgroundColor = [SiteOneController myColor1];

        [button addTarget:self action:@selector(showCCView:)
        forControlEvents:UIControlEventTouchUpInside];
         [button setTag:j];

         [self.view addSubview: button];
         j++;
     }

The @Selector methods is here:

- (void) showCCView:(id) sender {

    UIButton *button = (UIButton *)sender;
    int whichButton = button.tag;
    NSString* myNewString = [NSString stringWithFormat:@"%d", whichButton];
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view.backgroundColor = [UIColor whiteColor];

    UINavigationBar *cc = [SiteOneController myNavBar1:@"Constitution Center Content"];
    UINavigationBar *fc = [SiteOneController myNavBar1:@"Franklin Court Content"];
    UINavigationBar *ph = [SiteOneController myNavBar1:@"Presidents House Content"];

    if (whichButton = 0) 
    {
        NSLog(myNewString);
        [self.view addSubview:cc];
    }
    else if (whichButton = 1) 
    {
        NSLog(myNewString);
        [self.view addSubview:fc];
    }
    else if (whichButton = 2) 
    {
        NSLog(myNewString);
        [self.view addSubview:ph];
    }
}

现在,正如该方法所示,它正在将正确的ton子印到国家记录仪上,但电子记录仪“Franklin法院”作为标题“Franklin法院”显示一条航标,即使我点击了丁顿0时,它却说“Button 0点击”在哥尔,但如果(Button = 1)代码的话,它仍能做其他工作。

我在这里找不到什么东西?

最佳回答

请在您的条件下使用<代码>=的操作员而不是=的操作员,从而进行转让而不是比较。

由于转让的返还价值是分配值,第1条<代码>,其中Button改为0,对<代码>false<>/code>进行评价,然后将<代码>改为“Button,并对真实性进行评价,而不论你如何最终选择富兰克法院。

问题回答

您应使用<条码>=而不是<条码>。

不这样做:

    if (whichButton = 0) 
    {
        NSLog(myNewString);
        [self.view addSubview:cc];
    }
    ...

为此:

    if (whichButton == 0) 
    {
        NSLog(myNewString);
        [self.view addSubview:cc];
    }
    else if (whichButton == 1) 
    {
        NSLog(myNewString);
        [self.view addSubview:fc];
    }
    else if (whichButton == 2) 
    {
        NSLog(myNewString);
        [self.view addSubview:ph];
    }

或者:


NSLog (myNewString); //occurs in all cases.

switch (whichButton)
{
    case 0:
        [self.view addSubview:cc];
        break;
    case 1:
        [self.view addSubview:fc];
        break;
    case 2:
        [self.view addSubview:ph];
        break;
    default:
        // optionally handle the case where the button s tag was not 0, 1, or 2.
}




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签