2011-10-01 60 views
1

我創建的字體列表通過tableView,當用戶選項卡的任何單元格的字符串應根據單元格更改,但我不知道爲什麼我的代碼不起作用! 我的tableview與UIPopOverViewContoller打開:這裏是我的代碼:iOS didSelectRow和setString

.H

@interface FontViewController : UITableViewController <UITableViewDelegate , UITableViewDataSource , UINavigationControllerDelegate> { 

     NSMutableArray *listOfFonts; 

    } 
@property (nonatomic, retain) NSMutableArray *listOfFonts; 

.M

#import "FontViewController.h" 

#import "xxxAppDelegate.h" 
#import "xxxViewController.h" 

@implementation FontViewController 


- (void)viewDidLoad 
{ 

    listOfFonts = [[NSMutableArray alloc] init]; 
    [listOfFonts addObject:@"font1"]; 
    [listOfFonts addObject:@"font2"]; 
    [listOfFonts addObject:@"font3"]; 
    [listOfFonts addObject:@"font4"]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    //TableView Codes .... 

    cell.textLabel.text = [listOfFonts objectAtIndex:indexPath.row]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    xxxAppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate]; 

    appDelegate.viewController.detailItem = [listOfFonts objectAtIndex:indexPath.row];  
} 

xxxViewController.h:

@interface { 

NSString *fontString; 
    id detailItem; 

} 


@property (nonatomic ,retain) NSString *fontString; 
@property (nonatomic, retain) id detailItem; 

xxxViewController.m:

@synthesize detailItem ,fontString; 



- (void)setDetailItem:(id)newDetailItem { 

    if (detailItem != newDetailItem) { 
     [detailItem release]; 
     detailItem = [newDetailItem retain]; 

     //---update the view--- 
     fontString = [detailItem description]; 
    } 

} 

//here is the custom font codes : (I am using special fonts which does not works with UIApplicationFont in app plist) 


(CTFontRef)newCustomFontWithName:(NSString *)fontName 
          ofType:(NSString *)type 
         attributes:(NSDictionary *)attributes 
{ 

// the string of cell should be equal to the pathForResource:fontString for example font1 , font 2 ... 

    NSString *fontPath = [[NSBundle mainBundle] pathForResource:fontString = [detailItem description] ofType:@"ttf"]; 

    NSData *data = [[NSData alloc] initWithContentsOfFile:fontPath]; 
    CGDataProviderRef fontProvider = CGDataProviderCreateWithCFData((CFDataRef)data); 
    [data release]; 

/// blah alah blah 

} 

這裏是setDetailItem調試信息

NSLog(@"fontString: %@, assigned: %@ in controller: %@", fontString, [detailItem description], self); 

2011-10-02 23:12:57.036 Medad[558:10d03] fontString: (null), assigned: font1 in controller: < myAppViewController: 0x7078a30> 
2011-10-02 23:12:58.015 Medad[558:10d03] fontString: (null), assigned: font2 in controller: < myAppViewController: 0x7078a30> 
2011-10-02 23:12:58.475 Medad[558:10d03] fontString: (null), assigned: font3 in controller: < myAppViewController: 0x7078a30> 
2011-10-02 23:13:00.365 Medad[558:10d03] fontString: (null), assigned: font4 in controller: < myAppViewController: 0x7078a30> 

我的問題是這樣的一行: 的FontString這應該改變小區選擇...

NSString *fontPath = [[NSBundle mainBundle] pathForResource:???fontString????? ofType:@"ttf"]; 

,我將不勝感激,如果有人幫助我感謝

+0

「我的密碼不起作用!」沒有幫助,請解釋在哪裏以及哪些方面無法解決問題。如果可能,請提供來自GDB的錯誤消息。 – chown

+0

沒有任何錯誤和警告或崩潰:)只是當我選擇一個單元格的字符串不會改變它意味着不會改變字體 –

+0

我還會讓你注意到你將detailItem設置爲NSString「font1」或別人,所以你可以直接設置fontString屬性。沒有必要指定一個字符串,然後調用[description]來獲取相同的字符串。 – Zmaster

回答

1

在代碼的不同位置設置斷點,特別是在tableView:didSelectRowAtIndexPath:setDetailItem:方法來檢查它們是否被調用。

  • 如果他們沒有,你可能忘了把的UITableView的delegate(您可能已經確立了自己的viewController作爲tableview中的dataSource卻忘了delegate?)。

  • 如果是這樣,檢查在調試器(或使用NSLog)的各種對象的值,以檢查是否存在nil對象(將消息發送到nil沒有作用)

我會打賭你忘記將UITableViewdelegate插座連接到正確的對象(通常是您的ViewController)。 如果這不是原因,我們肯定需要更多信息,並且需要使用斷點和調試器一步一步地調試代碼。

+0

細胞選擇很好!根據'NSLog(@「單元格選擇:%@」,[listOfFonts objectAtIndex:indexPath.row]);' –

+0

我認爲主要問題是'NSString * fontPath = [[NSBundle mainBundle] pathForResource:fontString = [detailItem description ] ofType:@「ttf」];' –

+0

哇,沒有看到這行......但是WTF和'fontString = [detailItem description]'作用在傳遞給方法調用的參數中間嗎?!?這是無稽之談!你不應該在這裏使用'fontName'參數嗎? – AliSoftware