开发者

Objective-C / iPhone: changing TextView.text problem

Why 2 strings concat only when run for the 1st time?

- (void)viewDidLoad {
    NSInteger tmpCurrImg  = 1;
    NSString *tmp1 = [[[someclass instance] getSomeValAtPos:tmpCurrImg];    
    NSString *tmp2 = [[[someclass instance] getOtherValAtPos:tmpCurrImg];
    txt = [tmp1 stringByAppendingString:tmp2];
}

txt is NSString defined in header. Singleton works good, tmp1 and tmp2 point to exactly what I want. SegmentedControl starts this function:

- (IBAction)changeText {
    if (txtChooser.selectedSegmentIndex == 0) {
        txtView.text = txt;
    }
    if (txtChooser.selectedSegmentIndex == 1) {
        txtView.text = txt;
    }
}

When I start this View it displays 2 merged strings. When I switch back to t开发者_如何学Gohis view with SegmentedControl button the app crashes. If I use just tmp1 or tmp2 it works but when I try to call the merged string it doesn't work. Do I forget about something with pointers?


You have to retain txt if you expect it to stay alive beyond the end of viewDidLoad.


stringByAppendingString return string that was autoreleased, so you must retain it. All methods that names begin as class name, always return autorelease objects.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜