我是iOS开发的新手,这是我关于SO的第一个问题。如果这是一个愚蠢的问题,我道歉。
我没有使用XIB。我的目标是在设备旋转时按一个按钮并移动其位置。我的问题是,在旋转发生后,我无法找到更新/刷新屏幕上按钮“框架”的方法。
以下是我正在使用的04个文件。我已经阅读了其他几个SO问题,但我无法解决这个问题。谢谢大家的任何建议。
LV_AppDelegate.h
#import <UIKit/UIKit.h>
//
@class LV_ViewController;
//
@interface LV_AppDelegate : UIResponder <UIApplicationDelegate>
//
@property (strong, nonatomic) UIWindow *window;
//
@property (strong, nonatomic) LV_ViewController *viewController;
//
@end
LV_AppDelegate.m
#import "LV_AppDelegate.h"
#import "LV_ViewController.h"
//
@implementation LV_AppDelegate
//
@synthesize window = _window;
@synthesize viewController = _viewController;
//
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
//
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//
NSLog(@"Entering didFinishLaunchingWithOptions");
//
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[LV_ViewController alloc] initWithNibName:@"LV_ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[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
{
}
//
@end
LV_ViewController.h
#import <UIKit/UIKit.h>
//
@interface LV_ViewController : UIViewController {
UIButton *button_Narr_Places;
}
//
@property (nonatomic, retain) UIButton *button_Narr_Places;
//
@end
LV_ViewController.m
#import "LV_ViewController.h"
//
//
// - - - - - Open errors: - - - - -
/*
a.) Local declaration of 'button_Narr_Places' hides instance variable.
b.) If launched in Landscape, still thinks the device is in Portrait mode, and then
rotates to Landscape.
Launched in Portrait
Moved to Landscape
c.) Can not see button once screen is rotated. How to refresh the button's "frame" position after it's updated by "willAnimateRotationToInterfaceOrientation"?
*/
// - - - - -
@implementation LV_ViewController
//
@synthesize button_Narr_Places;
//
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
//
- (void)viewDidLoad
{
[super viewDidLoad];
}
//
- (void)loadView
{
// - - Create the "view" canvas - -
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
view.backgroundColor = [UIColor whiteColor];
//
// - - - - - Create a button - - - - -
UIButton *button_Narr_Places = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//
// - - - Adjust position based on INITIAL device orientation - - -
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
//
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
NSLog(@"Launched in Portrait");
button_Narr_Places.frame = CGRectMake(10, 200, 100, 40);
}
else if (orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft) {
NSLog(@"Launched in Landscape");
button_Narr_Places.frame = CGRectMake(50, 400, 100, 40);
}
// - - - Add button's title, color, selector - - -
[button_Narr_Places setTitle:@"Places Lived" forState:UIControlStateNormal];
button_Narr_Places.backgroundColor = [UIColor clearColor];
button_Narr_Places.tag = 2000;
[button_Narr_Places addTarget:self action:@selector(method_Narr_Places:)
forControlEvents:UIControlEventTouchUpInside];
//
[view addSubview:button_Narr_Places];
//
// -- Create a "label" view:
CGRect frame = CGRectMake(10, 85, 300, 20);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"Verdana" size:20];
label.text = @"Test ViewController.";
label.tag = 1000;
//
[view addSubview:label];
[label release];
self.view = view;
}
//
// - - - - - Method called by button above - - - - -
- (IBAction)method_Narr_Places:(id)sender
{
NSLog(@"Entering method_Narr_Places");
}
//
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationPortrait) {
NSLog(@"Moved to Portrait");
//
// [button_Narr_Places setFrame:CGRectMake( 10, 778, 100, 40)];
button_Narr_Places.frame = CGRectMake( 10, 200, 100, 40);
}
else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
NSLog(@"Moved to Portrait UpsideDown");
}
//
else if (orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft) {
NSLog(@"Moved to Landscape");
//
// [button_Narr_Places setFrame:CGRectMake(778, 100, 100, 40)];
button_Narr_Places.frame = CGRectMake(50, 400, 100, 40);
}
}
//
// - - - - - Boilerplate methods below - - - - -
//
- (void)viewDidUnload
{
[super viewDidUnload];
}
//
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
//
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
//
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
//
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
//
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
//
@end
答案 0 :(得分:1)
在LV_ViewController.m - loadView
函数中,您正在声明按钮的(本地)变量,如下所示:
UIButton *button_Narr_Places = [UIButton buttonWithType:UIButtonTypeRoundedRect];
这将创建按钮的临时本地副本(“隐藏”全局属性,以便您无法在此函数中访问它),然后在函数末尾将其抛出。如果您希望以后能够引用它(例如在willAnimateRotationToInterfaceOrientation
中),则需要使用在头文件中定义的属性:
button_Narr_Places = [UIButton buttonWithType:UIButtonTypeRoundedRect];
甚至更好:
self.button_Narr_Places = [UIButton buttonWithType:UIButtonTypeRoundedRect];
请注意,除非您有其他原因,否则您应该使用self.
表示法通过其访问者而非直接访问ivar。