我是通过 tableView 创建的字体列表,当用户选择任何单元格时,字体字符串应该根据单元格进行更改,但我不知道为什么我的代码不起作用! 我的 tableview 使用 UIPopOverViewContoller 打开:这是我的代码:
.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"];
如果有人帮助我,我将不胜感激
最佳答案
在代码的不同位置设置断点,特别是在 tableView:didSelectRowAtIndexPath:
和 setDetailItem:
方法中检查它们是否被调用。
如果不是,您可能忘记设置 UITableView 的
delegate
(您可能已将 viewController 设置为 tableview 的dataSource
但忘记了委托(delegate)
?)。如果是,检查调试器(或使用
NSLog
)各种对象的值以检查是否有nil
对象(发送消息为nil
无效)
我敢打赌,您忘记了将 UITableView
的 delegate
导出链接到正确的对象(通常是您的 ViewController)。 如果这不是原因,我们肯定需要更多信息,并且您需要使用断点和调试器逐步调试您的代码。
关于iOS didSelectRow 和 setString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620842/