正確なコード (NDA) を投稿することはできませんが、時間を取って、設計と実装に関してどのように機能するかを説明できます。
まず、私が現在行っているのは、静的ページの作成とトリガーされるイベントの作成によるフロントエンドの作業だけです。
私が書いてきたページの多くは、非常によく似た特性を持っていました。これらには以下が含まれます:
- 同じ背景効果と画像。
- 背景付きのナビゲーション バーと、現在どのページにいるかをユーザーに知らせるナビゲーション バーのタイトル。
- カスタマイズ用の追加のビュー。
このため、私が作成したページが継承できると感じた独自のカスタム ViewController を作成することにしました。私が書いた最初の 2ページでは、すべてが計画どおりに機能しました。私がしなければならなかったのは、クラスを継承し、設定を指定することだけでした。親の ViewController は、目的のテンプレートのレンダリングに関して他のすべてを処理します。戻るボタンは、本来あるべき場所に正確に表示され、それを押すと、ViewController スタックから外れて、ホームページに戻るだけでした。
次のように機能します。
- 親クラスは
NSMutableDictionary*
( として知られる)_additionalViews
を保持@protected
し、ページ作成の開始時に - を介して初期化されます。これには、親クラスとそのすべての子クラスの[super initWithNibName]
オーバーロードされた追加withNavTitle:(NSString*)navTitle
パラメーターがあります。 - 子クラスは、
_additionalViews
初期化時に関連するキーを使用してビューを追加することにより、作成するビューを指定するだけです。メソッドが呼び出されるとすぐに、キーによるviewDidLoad
反復処理と呼び出しによってすべてがロードされます_additionalViews
[self.view addSubview:[_additionalViews objectForKey:key]]
- 戻るボタンが押されると、オブジェクトが viewController スタックからポップされるイベントは、子クラスではなく、親クラスによって管理および起動されます。
ご覧のとおり、このアイデアにより、多くのコードの肥大化が軽減され、再利用可能に保たれます。この方法論で書いた最初の 2 つのページは、基本的に静的な情報ページであり、戻るボタンはそれらと完全に連携します (Web ページへのリンクや、私が投げた他のさまざまなイベントも同様です)。
ただし、この新しいページを作成すると、すべてがレンダリングされますが、機能はありません。前の 2 ページで行ったのと同じ方法ですべてを行っています。これはなぜですか?
私にできることは、ボタンやリンクなどのために作成したいくつかのユーティリティ関数を提供することです。これらの中で、おそらく良い考えではない何かを見た人がいたら、フィードバックをいただければ幸いです。それらは間違いなくレンダリングされ、最初の 2 ページで機能します。この最後のものだけではありません。
助けていただきありがとうございます (Objective-C で書き始めてまだ 1.5 か月なので、まだかなりの初心者ですが、C++ のバックグラウンドは十分にあります)。
コード - ヘッダーとソースの組み合わせ
#import <UIKit/UIKit.h>
typedef struct __TextAttributes__ {
UIColor* textColor;
bool isBold;
CGFloat fontSize;
NSString* text;
} TextAttributes;
typedef struct __ButtonAttributes__ {
UIControlState controlState;
UIControlEvents controlEvents;
NSString* imgPath;
id target;
SEL selector;
} ButtonAttributes;
UITextView* UITextView_Make_NoEdit(NSString* displayText, CGFloat fontSize, bool useBoldFont, CGRect frameDims, UIColor* color, id delegate);
UIButton* Link_Make(NSString* linkText, CGFloat fontSize, bool useBoldFont,
SEL actionSelector, id target, CGRect frameDims);
UIButton* Button_Make_Custom(TextAttributes* ta, ButtonAttributes* attr, CGRect frameDims, bool freeBtnAttr, bool freeTextAttr);
ButtonAttributes* ButtonAttributes_Make(NSString*imgName, NSString* imgType, UIControlState controlState, UIControlEvents controlEvents, id target, SEL selector);
void ButtonAttributes_Free(ButtonAttributes* attr);
TextAttributes* TextAttributes_Make(NSString* text, UIColor* textColor, CGFloat fontSize, bool isBold);
void TextAttributes_Free(TextAttributes* attr);
//-------------------
// Global Functions
//-------------------
//----------------------------------------
static const float TextFont_Alpha = 0.75;
//----------------------------------------
typedef enum __MemoryType__ {
MemoryTypeTextAttributes,
MemoryTypeButtonAttributes
} MemoryType;
static void CheckMemAndThrow(void* mem, MemoryType mt )
{
if (mem)
return;
NSString *memTypeAndFunc, *msg;
switch (mt) {
case MemoryTypeButtonAttributes:
memTypeAndFunc = @"ButtonAttributes in ButtonAttributes_Make(...)";
break;
case MemoryTypeTextAttributes:
memTypeAndFunc = @"TextAttributes in TextAttributes_Make(...)";
break;
}
msg = [NSString stringWithFormat:@"Memory allocation error for %@", [memTypeAndFunc autorelease]];
@throw [[NSException alloc] initWithName:@"MemoryAllocException" reason:msg userInfo:nil];
}
UITextView* UITextView_Make_NoEdit(NSString* displayText, CGFloat fontSize, bool useBoldFont, CGRect frameDims, UIColor* color, id delegate)
{
UITextView* view = [[UITextView alloc] initWithFrame:frameDims];
[view setText:displayText];
UIFont* font;
if (useBoldFont)
{
font = [UIFont boldSystemFontOfSize:fontSize];
}
else
{
font = [UIFont systemFontOfSize:fontSize];
}
[view setTextColor:color];
[view setDelegate:delegate];
[view setEditable:NO];
[view setScrollEnabled:NO];
[view setBackgroundColor:[UIColor clearColor]];
[view setFont:font];
[font release];
return view;
}
static const CGFloat Default_Link_Red = 1.0f / 255.0f;
static const CGFloat Default_Link_Green = 184.0f / 255.0f;
static const CGFloat Default_Link_Blue = 252.0f / 255.0f;
static const CGFloat Default_Link_Alpha = 1.0f;
UIButton* Link_Make(NSString* linkText,
CGFloat fontSize,
bool useBoldFont,
SEL actionSelector, id target,
CGRect frameDims)
{
UIButton* linkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[linkButton setFrame:frameDims];
[linkButton setTitleColor:[UIColor colorWithRed:Default_Link_Red
green:Default_Link_Green
blue:Default_Link_Blue
alpha:Default_Link_Alpha]
forState:UIControlStateNormal];
[linkButton setTitle:linkText forState:UIControlStateNormal];
UIFont* font;
if (useBoldFont)
{
font = [UIFont boldSystemFontOfSize:fontSize];
}
else
{
font = [UIFont systemFontOfSize:fontSize];
}
[linkButton.titleLabel setFont:[font retain]];
[linkButton addTarget:target action:actionSelector forControlEvents:UIControlEventTouchDown];
[font release];
return linkButton;
}
UIButton* Button_Make_Custom(TextAttributes* attr, ButtonAttributes* buttonAttr, CGRect frameDims, bool freeBtnAttr, bool freeTextAttr)
{
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:attr->text forState:buttonAttr->controlState];
[btn setTitleColor:attr->textColor forState:buttonAttr->controlState];
[btn setBackgroundColor:[UIColor clearColor]];
if (attr->isBold)
{
[btn.titleLabel setFont:[UIFont boldSystemFontOfSize:attr->fontSize]];
}
else
{
[btn.titleLabel setFont:[UIFont systemFontOfSize:attr->fontSize]];
}
[btn setFrame:frameDims];
[btn setBackgroundImage:[UIImage imageWithContentsOfFile:[buttonAttr->imgPath retain]] forState:buttonAttr->controlState];
[btn addTarget:buttonAttr->target
action:buttonAttr->selector
forControlEvents:buttonAttr->controlEvents];
if (freeTextAttr)
TextAttributes_Free(attr);
if (freeBtnAttr)
ButtonAttributes_Free(buttonAttr);
if (![btn isEnabled])
[btn setEnabled:YES];
return btn;
}
ButtonAttributes* ButtonAttributes_Make(NSString* imgName, NSString* imgType, UIControlState controlState, UIControlEvents controlEvents, id target, SEL selector)
{
ButtonAttributes* btnAttr = (ButtonAttributes *)calloc(1, sizeof(ButtonAttributes));
CheckMemAndThrow((void*)btnAttr, MemoryTypeButtonAttributes);
btnAttr->imgPath = [[NSBundle mainBundle] pathForResource:imgName ofType:imgType];
btnAttr->controlState = controlState;
btnAttr->controlEvents = controlEvents;
btnAttr->target = target;
btnAttr->selector = selector;
return btnAttr;
}
void ButtonAttributes_Free(ButtonAttributes* attr)
{
if (!attr)
return;
if (attr->imgPath)
[attr->imgPath release];
free(attr);
}
TextAttributes* TextAttributes_Make(NSString* text, UIColor* textColor, CGFloat fontSize, bool isBold)
{
TextAttributes* textAttributes = (TextAttributes *)calloc(1, sizeof(TextAttributes));
CheckMemAndThrow((void*)textAttributes, MemoryTypeTextAttributes);
textAttributes->text = text;
textAttributes->textColor = textColor;
textAttributes->fontSize = fontSize;
textAttributes->isBold = isBold;
return textAttributes;
}
void TextAttributes_Free(TextAttributes* attr)
{
if (!attr)
return;
if (attr->text) {
[attr->text release];
}
if (attr->textColor) {
[attr->textColor release];
}
free(attr);
}