小编JAA*_*JAA的帖子

在NSUserDefaults保存的NSMutableArray中插入对象

我有NSUserDefaults保存的NSMutableArray.这个数组是我用户可以保存的"最喜欢的"项目,所以当我想添加一个项目时,我需要读取数组(来自NSuserDefault)并保存在第一个空闲位置.

我正在使用此方法在NSMutableArray中添加值

-(IBAction)save{
   NSMutableArray *abc = [[NSUserDefaults standardUserDefaults] objectForKey:@"12345"];
   int n = [abc count];
   [abc insertObject:@"aaa" atIndex:n];
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   [[NSUserDefaults standardUserDefaults] setObject:abc forKey:@"12345"];
   [defaults synchronize];
   [abc release];
}
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?如果用户调用此方法两次,则第二次应用程序崩溃时会显示以下日志:

*由于未捕获的异常'NSInternalInconsistencyException'终止应用程序,原因:' - [__ NSCFArray insertObject:atIndex:]:发送到immutable对象的mutating方法'

为什么?为什么只是第二次?第一次工作正常!

iphone nsuserdefaults nsmutablearray

17
推荐指数
2
解决办法
2万
查看次数

获取NSString高度

我有一个NSString,我想知道它的高度来创建一个合适的UILabel.

这样做

NSString *string = @"this is an example"; 
CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:10.0f] 
                          forWidth:353.0 
                     lineBreakMode:UILineBreakModeWordWrap];
float height = size.height;
Run Code Online (Sandbox Code Playgroud)

身高现在是13.0.如果我使用这个字符串

NSString *string = @"this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example this is an …
Run Code Online (Sandbox Code Playgroud)

iphone nsstring uilabel sizewithfont ios

11
推荐指数
1
解决办法
1万
查看次数

键盘隐藏了TabBar

我正在使用TabBar应用程序.在一个视图中有一个UISearchBar,当按下时,键盘出现.

问题是键盘隐藏了标签栏.

你知道怎么解决吗?

iphone tabbar uitabbar iphone-softkeyboard

8
推荐指数
2
解决办法
3616
查看次数

当用户收到推送通知时,打开特定的选项卡/视图

我想做Twitter应用程序之类的东西:当有人写我时,我会收到推送通知; 如果我"滑动通知"应用程序启动,但不是在普通流中,它会在特定视图中以某人写信给我的推文开始!

在我的应用程序中,我有类似RSS阅读器的东西,当有新消息时推送通知到达.所以,我想打开"单一新闻视图",而不是主视图(现在正在发生).

我能做什么?

谢谢

iphone push push-notification apple-push-notifications ios

6
推荐指数
1
解决办法
6980
查看次数

当deviceOrientation改变时,在UITableView中重新加载数据

我正在使用带有UITableViewCell的UITableView.我的应用程序支持横向,所以我创建了2个UITableViewCell:一个用于portraid,另一个用于横向.

在我的cellForRowAtIndexPath方法中,这是我的代码

static NSString *CellIdentifier;
static NSString *NibNamed;

UIDeviceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation;

if (deviceOrientation != UIDeviceOrientationLandscapeLeft &&
    deviceOrientation != UIDeviceOrientationLandscapeRight)
{
    CellIdentifier= @"Cell1";
    NibNamed = @"cell_news";
}
else
{
    CellIdentifier= @"Cell2";
    NibNamed = @"cell_news_landscape";
}

[[NSBundle mainBundle] loadNibNamed:NibNamed owner:self options:NULL];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:NibNamed owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
///here i add title, description, ecc... in my UITableViewCell
Run Code Online (Sandbox Code Playgroud)

然后在shouldAutorotateToInterfaceOrientation我写了这个:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[my_table reloadData];
return …
Run Code Online (Sandbox Code Playgroud)

iphone landscape uitableview

5
推荐指数
1
解决办法
1万
查看次数

缩小时,在屏幕上居中显示UIImageView

我在UIScrollView中有一个UIImageView.我希望用户可以缩放和导航图像.

这是我的工作代码:

//img is the UIImageView
//scroller is the UIScrollView

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return img; 
}
- (void)viewDidLoad {
   [super viewDidLoad];
   UIImage *image = [UIImage imageNamed:@"map_screen.png"];
   img = [[UIImageView alloc] initWithImage:image];
   scroller.delegate = self;
   scroller.autoresizesSubviews = YES;
   scroller.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
   scroller.contentSize = img.frame.size;
   scroller.scrollEnabled = YES;
   scroller.directionalLockEnabled = NO;
   scroller.userInteractionEnabled = YES;

   CGSize ivsize = img.frame.size;
   CGSize ssize = scroller.frame.size;

   float scalex = ssize.width / ivsize.width;
   float scaley = ssize.height / ivsize.height;
   scroller.minimumZoomScale = fmin(1.0, fmax(scaley, …
Run Code Online (Sandbox Code Playgroud)

iphone uiscrollview uiimageview image-zoom

5
推荐指数
1
解决办法
4693
查看次数

UITableview中的第一行,与其他行不同

我的UITableView需要在第一行中有一个图像.有什么问题?当用户向下滚动桌面视图时,他向上滚动,在图像上方有其他行的信息!你知道为什么吗?

这是我的代码(我使用的是UITableViewCell)

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row != 0) {
    return 73.0;
}
else {
    return 109.0;
}   
}

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    if (indexPath.row == 0) {
        [[NSBundle mainBundle] loadNibNamed:@"customcell_3" owner:self options:NULL];
        cell = cellaNib_with_image;
    }
    else {
        [[NSBundle mainBundle] loadNibNamed:@"customcell_2" owner:self options:NULL];
        cell = cellaNib;
    }


}

if (indexPath.row == 0) {
    UIImage *rowBackground;
    UIImage *selectionBackground;
    rowBackground …
Run Code Online (Sandbox Code Playgroud)

iphone uitableview

3
推荐指数
1
解决办法
8693
查看次数

了解更新UIprogressView的下载进度

我的应用程序从互联网下载视频并将其保存在iPhone中.

我正在使用此代码

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setHTTPMethod:@"GET"];
NSError *error;
NSURLResponse *response;


NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *videosFolderPath = [documentFolderPath stringByAppendingPathComponent:@"videos"]; 

//Check if the videos folder already exists, if not, create it!!!
BOOL isDir;
if (([fileManager fileExistsAtPath:videosFolderPath isDirectory:&isDir] && isDir) == FALSE) {
    [[NSFileManager defaultManager] createDirectoryAtPath:videosFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
}

NSString *filePath = [videosFolderPath stringByAppendingPathComponent:@"name.mp4"];

if ([fileManager fileExistsAtPath:filePath ] == YES) {
    NSLog (@"File exists");
}
else {
    NSLog (@"File not …
Run Code Online (Sandbox Code Playgroud)

iphone download uiprogressview ios

2
推荐指数
1
解决办法
5095
查看次数