Xcode/iPhone:第一个基于Windows的应用程序 - 为什么TabBarController视图中没有显示任何东西

使用基于Windows的模板时,是否需要记住一些事情?因为我不清楚为什么选项卡显示出来,但视图中没有任何内容显示出来。Xcode/iPhone:第一个基于Windows的应用程序 - 为什么TabBarController视图中没有显示任何东西

你能帮忙吗?因为我现在已经搜索了几个小时之前的问题,而且我还没有找到任何可以解决的问题。

AnotherMadeUpAppDelegate.h

#import <UIKit/UIKit.h> 

#import "AnotherMadeUpViewController.h"

@interface AnotherMadeUpAppDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

AnotherMadeUpAppDelegate.m

#import "AnotherMadeUpAppDelegate.h" 

@implementation AnotherMadeUpAppDelegate

@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

// Override point for customization after application launch.

UIViewController *vc1 = [[UIViewController alloc] init];

UIViewController *vc2 = [[UIViewController alloc] init];

AnotherMadeUpViewController *vc3 = [[AnotherMadeUpViewController alloc] init];

UITabBarController *tbc = [[UITabBarController alloc] init];

tbc.viewControllers = [NSArray arrayWithObjects:vc1, vc2, vc3, nil];

[vc1 release];

[vc2 release];

[vc3 release];

[self.window addSubview:tbc.view];

[self.window makeKeyAndVisible];

return YES;

}

...

@end

AnotherMadeUpViewController.h

#import <UIKit/UIKit.h> 

@interface AnotherMadeUpViewController : UIViewController<UIScrollViewDelegate>

{

IBOutlet UIPageControl *pageControl;

IBOutlet UIScrollView *scroller;

IBOutlet UILabel *label;

}

@property (nonatomic,retain)IBOutlet UIPageControl *pageControl;

@property (nonatomic,retain)IBOutlet UIScrollView *scroller;

@property (nonatomic,retain)IBOutlet UILabel *label;

-(IBAction)clickPageControl:(id)sender;

@end

AnotherMadeUpViewController.m

#import "AnotherMadeUpViewController.h" 

@implementation AnotherMadeUpViewController

@synthesize pageControl,scroller,label;

-(IBAction)clickPageControl:(id)sender

{

int page=pageControl.currentPage;

CGRect frame=scroller.frame;

frame.origin.x = frame.size.width * page;

frame.origin.y = 0;

[scroller scrollRectToVisible:frame animated:YES];

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

int page = scrollView.contentOffset.x/scrollView.frame.size.width;

pageControl.currentPage=page;

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}

- (void)dealloc

{

[super dealloc];

[label release];

}

- (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.

}

#pragma mark - View lifecycle

- (void)viewDidLoad

{

[super viewDidLoad];

scroller.pagingEnabled=YES;

CGFloat labelOriginX = label.frame.origin.x;

CGFloat labelOriginY = label.frame.origin.y;

CGFloat scrollWidth = 0;

int pageNumber = 0;

for (int i=0; i<9; i++)

{

CGRect rect = label.frame;

rect.size.height = label.frame.size.height;

rect.size.width = label.frame.size.width;

rect.origin.x = labelOriginX + scrollWidth;

rect.origin.y = labelOriginY;

label.frame = rect;

label.text = [NSString stringWithFormat:@"%d", pageNumber];

label.textColor = [UIColor redColor];

[scroller addSubview:label];

pageNumber++;

scrollWidth += scroller.frame.size.width;

}

scroller.delegate=self;

scroller.directionalLockEnabled=YES;

scroller.showsHorizontalScrollIndicator=NO;

scroller.showsVerticalScrollIndicator=NO;

pageControl.numberOfPages=9;

pageControl.currentPage=0;

scroller.contentSize=CGSizeMake(pageControl.numberOfPages*self.view.frame.size.width, self.view.frame.size.height);

[self.view addSubview:scroller];

}

- (void)viewDidUnload

{

[super viewDidUnload];

[label release];

self.label = nil;

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

@end

回答:

解剖你的viewDidLoad -

scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)]; 

你似乎在这里创建一个新的滚动视图的实例,但你必须宣布它作为一个出口。如果您没有插座,请删除scrollerIBOutlet标签。如果你确实有一个并想使用它,那么删除上面的行。这样做将是一个较短的方式,

scroller = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 

的另一件事是要创建10个标签,但不分配框架。向他们展示各自在不同的页面之一,

int pageNumber = 0; 

for (int i = 0; i < 10; i++)

{

UILabel *label = [[UILabel alloc] init];

[label sizeToFit];

label.center = CGPointMake (((2 * i + 1) * self.view.frame.size.width)/2, self.view.frame.size.height/2);

label.text = [NSString stringWithFormat:@"%d", pageNumber];

[scroller addSubview:label];

[label release];

pageNumber++;

}

后来设置contentSize显示10页,

scroller.contentSize = CGSizeMake(10 * self.view.frame.size.width, self.view.frame.size.height);  

回答:

问题是这一行

AnotherMadeUpViewController *vc3 = [[AnotherMadeUpViewController alloc] init]; 

您需要将其更改为

AnotherMadeUpViewController *vc3 = [[AnotherMadeUpViewController alloc] initWithNibName:@"AnotherMadeUpViewController" bundle:nil]; 

然后您的.xib将被加载并且您的插座将被连接。

并且不要忘记将你的零售店连接到IB的文件所有者。

以上是 Xcode/iPhone:第一个基于Windows的应用程序 - 为什么TabBarController视图中没有显示任何东西 的全部内容, 来源链接: utcz.com/qa/266499.html

回到顶部