我正在开发一个非常简单的iOS应用程序,让我开始编程。它是一个基于选项卡的应用程序,因此有一个MainWindow.xib以及FirstView.xib和SecondView.xib。所有这一切都发生在第一个视图中。我想在第一个视图中添加一个进度条,当我添加对象时,它将自己附加到FirstView.xib并显示并让我移动它。要进行测试,alpha设置为1.00,进度设置为0.5。无论如何,无论我做什么,它都不会出现。我做错了什么?
AppDelegate.m:
@synthesize window=_window;
@synthesize tabBarController=_tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Add the tab bar controller's current view as a subview of the window
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
- (void)dealloc
{
[_window release];
[_tabBarController release];
[super dealloc];
}
@end
FirstViewController.h:
#import <UIKit/UIKit.h>
NSTimer *stopWatchTimer;
NSDate *startDate;
@interface FirstViewController : UIViewController {
UILabel *label;
UILabel *stopWatchLabel;
UIProgressView *progressBar;
UIButton *topButton;
}
@property (nonatomic, retain) IBOutlet UILabel *stopWatchLabel;
@property (nonatomic, retain) IBOutlet UIProgressView *progressBar;
- (IBAction)onStartPressed:(id)sender;
- (IBAction)onStopPressed:(id)sender;
- (IBAction)onResetPressed:(id)sender;
@end
FirstViewController.m
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize progressBar;
@synthesize stopWatchLabel;
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.*/
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
[self setStopWatchLabel:nil];
[topButton release];
topButton = nil;
[super viewDidUnload];
// Release any[progress release];
progressBar = nil;
[progressBar release];
progressBar = nil;
[self setProgressBar:nil];
//retained subviews of the[self setProgressBar:nil];
// e.g. self.myOutlet = nil;
}
static NSInteger counter=0;
static NSInteger secs=0;
static NSInteger mins=0;
static NSInteger hrs=0;
- (void)dealloc
{
[stopWatchLabel release];
[label release];
[topButton release];
[super dealloc];
}
-(void)updateTimer {
//updates the timer }
-(void)clearTimer {
//clears the timer
}
-(void)stopTimer{
//stops the timer }
- (IBAction)onStartPressed:(id)sender {
//stopWatchLabel.text=@"Start Pressed";
progressBar.alpha=1.0;
//run timer }
- (IBAction)onStopPressed:(id)sender {
[self stopTimer];
}
- (IBAction)onResetPressed:(id)sender {
[self stopTimer];
[self clearTimer];
}
@end
答案 0 :(得分:2)
创建视图后,您需要做两件事:您必须将其添加为可见视图的子视图并设置适当的框架。
以下是如何做到这一点:
[self.view addSubview:progressBar];
progressBar.frame = CGRectMake(x, y, width, height);
编辑:可能与您的问题完全无关,但这都是错误的:
- (void)viewDidUnload
{
[self setStopWatchLabel:nil];
[topButton release];
topButton = nil;
[super viewDidUnload];
// Release any[progress release];
progressBar = nil;
[progressBar release];
progressBar = nil;
[self setProgressBar:nil];
//retained subviews of the[self setProgressBar:nil];
// e.g. self.myOutlet = nil;
}
您可能希望它看起来像以下内容:
- (void)viewDidUnload
{
[super viewDidUnload];
[self setStopWatchLabel:nil];
[topButton release];
topButton = nil;
[label release];
label = nil;
self.progressBar = nil;
}
确保你明白你做错了什么。这一点非常重要,否则您的应用程序将泄漏和/或崩溃。
你的其余代码并没有真正做任何事情。你似乎在做IB中的所有事情,所以我想这就是你的问题所在。