나는 코딩이 처음이므로 이것이 어리석은 질문이라면 용서하십시오.
내 앱에는 가입 페이지와 홈 화면이 있습니다. 가입 페이지에서 사용자는 자신의 이름과 이메일 ID를 입력하여 가입합니다. 그가 그렇게 할 때 사용자 개체를 정의하는 데 사용하는 다음 구조체가 있습니다.
public struct User {
let email: String
let name: String
}
let user = User(email: "[email protected]", name: "Paul")
이제이 사용자 개체는 signUp보기에서 정의되지만 해당 속성을 전역 적으로 참조 할 수 있기를 원합니다. 즉, 내 홈페이지에 다음과 같은 메시지를 포함 할 수 있기를 원합니다.
let welcomeMessage = "Welcome, \(user.name)"
내가 말하는 것이 가능합니까?
아니면 구조체 이외의 다른 것을 사용하여 내 User 구조체의 유일한 저장된 인스턴스에 전역 적으로 액세스해야합니까?
내가 당신의 질문을 이해할 수 있다면. 가입이 완료되면 user
홈 화면 (또는 다른 여러 장소)에서 해당 정보에 액세스하려고합니다 . 여러 가지 방법으로 그것을 달성 할 수 있습니다. 아래는 그 중 두 가지입니다.
다만,이 응답을 더 설명하게 고려해야 가입 화면으로 표현 SignUpViewController
하고 홈 화면으로 표시되고HomeViewController
user
가입이 완료되면 통과 할 수 있습니다. 가입이 완료되면 확신합니다. 당신은 초기화해야 HomeViewController
가입 화면 어딘가에 pushing
또는 presenting
을 HomeViewController
.
속성을 (예를 들어 보자 정의 user
유형) User
에서 HomeViewController
당신이를 이니셜 때 HomeViewController
가입 화면에서, 당신은 당신이 밀어 전에 통과 또는를 제시해야을 HomeViewController
.
예제 코드
// Your user struct
struct User {
let email: String
let name: String
}
class SignUpViewController: UIViewController {
//This is just to show you, I am sure you must be initializing the user appropriately.
let user = User(email: "[email protected]", name: "Paul")
//Assuming you have a function which is called as soon signup is completed
func didSignUpCompleted() {
// initialise the HomeViewController, here I am assuming you have storyboard corresponding to HomeViewController
let storyboard = UIStoryboard(name: "youstoryboarname", bundle: nil)
let homeViewController = storyboard.instantiateViewController(withIdentifier: "homeViewControllerIdentifier") as! HomeViewController
// This is the point where you have to pass that user property to HomeViewController
homeViewController.user = user
// If the viewcontroller is embeded in UINavigationController
self.navigationController?.pushViewController(homeViewController, animated: true)
}
}
class HomeViewController: UIViewController {
// this will be set from SignUp screen
var user: User?
func viewDidLoad() {
super.viewDidLoad()
guard let signedUpUser = user else { return }
let welcomeMessgae = "Welcome, \(signedUpUser.name)"
}
}
user
여러 위치 에서이 속성 이 필요할 수있는 사용 사례 가있는 경우이 user
정보 를 보유 할 싱글 톤 클래스를 만들 수 있습니다 . 정의 user
가 단일 클래스의 속성을 때 당신을 설정 pushing
또는 presenting
HomeViewController
에서 SignUpViewController
.
속성 AppSession
을 보유하는 싱글 톤 클래스 라고 가정 해 봅시다.user
class AppSession {
static let current = AppSession()
// user property. which will be set from sign up screen
var user: User?
private init() { }
}
가입이 완료되면 아래 함수가 호출된다고 가정합니다 (에서 정의 됨 SignUpViewController
).
func didSignUpCompleted() {
// set the `user`
AppSession.current.user = User(email: "[email protected]", name: "Paul")
}
나중에 이와 같은 곳에서 액세스 할 수 있습니다.
class HomeViewController: UIViewController {
func viewDidLoad() {
super.viewDidLoad()
guard let signedUpUser = AppSession.current.user else { return }
let welcomeMessgae = "Welcome, \(signedUpUser.name)"
}
}
사용 사례에 따라 언급 된 접근 방식 중 하나를 구현할 수 있도록 약간의 이해를 얻을 수 있기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다