我是通过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"];如果有人帮我,我将不胜感激
发布于 2011-10-01 23:16:27
在代码中的不同位置设置断点,特别是在tableView:didSelectRowAtIndexPath:和setDetailItem:方法中,以检查它们是否被调用。
delegate (您可能已将viewController设置为tableview的dataSource,但忘记了它们是什么,请在调试器中(或使用NSLog)检查各种对象的值,以检查是否存在nil对象(向nil发送消息没有任何效果)我敢打赌,您忘记将UITableView的delegate插座链接到适当的对象(通常是您的ViewController)。如果这不是原因,我们肯定需要更多信息,您需要使用断点和调试器一步一步地调试代码。
https://stackoverflow.com/questions/7620842
复制相似问题